Advertisement
Dodo67

ActivationController

Feb 11th, 2022
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.registerController = (req,res) => {
  2.    const{name , email , password} = req.body
  3.    //Extracts the validation errors from a request and makes them available in a Result object.
  4.    const errors = validationResult(req);
  5.  
  6.    if (!errors.isEmpty()) {
  7.        const firstError = errors.array().map(error => error.msg)[0]
  8.        return res.status(422).json({
  9.            error: firstError
  10.        })
  11.    }
  12.     else{
  13.         User.findOne({
  14.             email
  15.         }).exec((err , user) => {
  16.             //If user exists
  17.             if(user) {
  18.                 return res.status(400).json({
  19.                     error: "Email is taken"
  20.                 })
  21.             }
  22.         })
  23.  
  24.         //Generate Token
  25.         const token = jwt.sign({
  26.             name,
  27.             email,
  28.             password
  29.         },
  30.         process.env.JWT_ACCOUNT_ACTIVATION,
  31.         {
  32.             expiresIn: '15min'
  33.         }
  34.         )
  35.         const emailDataRegistration = {
  36.             from: process.env.EMAIL_FROM,
  37.             to : email,
  38.             subject: "Account activation link",
  39.             html: `
  40.             <h1>Please Click on link to activate </h1>
  41.             <p>${process.env.CLIENT_URL}/activate/${token}</p>
  42.             <hr/>
  43.             <p>This email contain sensitive info</p>
  44.             <p>${process.env.CLIENT_URL}
  45.                 `
  46.         }
  47.        
  48.         transporter.sendMail(emailDataRegistration , function(err,info)  {
  49.             if(err){
  50.                 console.log(err)
  51.                 return;
  52.             }
  53.             console.log("Sent: " + info.response)
  54.         })
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement