Advertisement
Dodo67

Mail

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