11const bcrypt = require ( 'bcrypt' ) ;
22const gravatar = require ( 'gravatar' ) ;
33const { User } = require ( '../../models' ) ;
4- const { getErrorMessage } = require ( '../../utils' ) ;
4+ const { getErrorMessage, sendEmail } = require ( '../../utils' ) ;
5+ const { v4 } = require ( 'uuid' ) ;
56
67const signup = async ( req , res ) => {
78 const { email, password, subscription } = req . body ;
@@ -12,6 +13,7 @@ const signup = async (req, res) => {
1213 . status ( 409 )
1314 . json ( getErrorMessage ( 409 , `Email: '${ email } ' in use` ) ) ;
1415 }
16+ const verificationToken = v4 ( ) ;
1517
1618 const avatarURL = gravatar . url ( email ) ;
1719 const hashPassword = bcrypt . hashSync ( password , bcrypt . genSaltSync ( 10 ) ) ;
@@ -20,15 +22,25 @@ const signup = async (req, res) => {
2022 email,
2123 password : hashPassword ,
2224 avatarURL,
25+ verificationToken,
2326 } ) ;
2427
28+ const mail = {
29+ to : email ,
30+ subject : 'Site registration confirmation' ,
31+ html : `<a target="_blank" href="http://localhost:3000/api/auth/verify/${ verificationToken } ">Click to confirm registration</a>` ,
32+ } ;
33+
34+ await sendEmail ( mail ) ;
35+
2536 res . status ( 201 ) . json ( {
2637 status : 'success' ,
2738 code : 201 ,
2839 user : {
2940 email : result . email ,
3041 subscription : result . subscription ,
3142 avatarURL : result . avatarURL ,
43+ verificationToken : result . verificationToken ,
3244 } ,
3345 } ) ;
3446} ;
0 commit comments