Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var smtpTransport = nodemailer.createTransport({
- service: "Gmail",
- auth: {
- user: "gmail",
- pass: "password"
- }
- });
- var rand,mailOptions,host,link;
- router.post('/register', AuthCtrl.CreateUser);
- router.post('/login', AuthCtrl.LoginUser);
- router.get('/send',function(req,res){
- rand=Math.floor((Math.random() * 100) + 54);
- host=req.get('host');
- link="http://"+req.get('host')+"/verify?id="+rand;
- mailOptions={
- to : req.query.to,
- subject : "Please confirm your Email account",
- html : "Hello,<br> Please Click on the link to verify your email.<br><a href="+link+">Click here to verify</a>"
- }
- console.log(mailOptions);
- smtpTransport.sendMail(mailOptions, function(error, response){
- if(error){
- console.log(error);
- res.end("error");
- }else{
- console.log("Message sent: " + response.message);
- res.end("sent");
- }
- });
- });
- router.get('/verify',function(req,res){
- console.log(req.protocol+":/"+req.get('host'));
- if((req.protocol+"://"+req.get('host'))==("http://"+host))
- {
- console.log("Domain is matched. Information is from Authentic email");
- if(req.query.id==rand)
- {
- console.log("email is verified");
- res.end("<h1>Email "+mailOptions.to+" is been Successfully verified");
- }
- else
- {
- console.log("email is not verified");
- res.end("<h1>Bad Request</h1>");
- }
- }
- else
- {
- res.end("<h1>Request is from unknown source");
- }
- });
- module.exports = router;
- module.exports = {
- async CreateUser(req, res) {
- const schema = Joi.object().keys({
- username: Joi.string()
- .required(),
- email: Joi.string()
- .email()
- .required(),
- password: Joi.string()
- .required(),
- birthday: Joi.number().integer()
- .required().min(2).max(2),
- birthmonth: Joi.number().integer()
- .required().min(2).max(2),
- birthyear: Joi.number().integer()
- .required(),
- age:age
- });
- const { error, value } = Joi.validate(req.body, schema);
- if (error && error.details) {
- return res.status(HttpStatus.BAD_REQUEST).json({ msg: error.details })
- }
- const userEmail = await User.findOne({
- email: Helpers.lowerCase(req.body.email)
- });
- if (userEmail) {
- return res
- .status(HttpStatus.CONFLICT)
- .json({ message: 'Email already exist' });
- }
- const userName = await User.findOne({
- username: Helpers.firstUpper(req.body.username)
- });
- if (userName) {
- return res
- .status(HttpStatus.CONFLICT)
- .json({ message: 'Username already exist' });
- }
- return bcrypt.hash(value.password, 10, (err, hash) => {
- if (err) {
- return res
- .status(HttpStatus.BAD_REQUEST)
- .json({ message: 'Error hashing password' });
- }
- const age = moment().diff(moment([birthyear, birthmonth - 1, birthday]), 'years');
- const body = {
- username: Helpers.firstUpper(value.username),
- email: Helpers.lowerCase(value.email),
- birthday: (value.bday),
- birthmonth: (value.month),
- birthyear: (value.month),
- password: hash,
- age:age
- };
- User.create(body)
- .then(user => {
- const token = jwt.sign({ data: user }, dbConfig.secret, {
- expiresIn: '5h'
- });
- res.cookie('auth', token);
- res
- .status(HttpStatus.CREATED)
- .json({ message: 'User created successfully', user, token });
- })
- .catch(err => {
- res
- .status(HttpStatus.INTERNAL_SERVER_ERROR)
- .json({ message: 'Error occured' });
- });
- });
- },
Add Comment
Please, Sign In to add comment