1
- import { HttpError } from "../helpers/index.js" ;
1
+ import { HttpError , sendEmail } from "../helpers/index.js" ;
2
2
import { ctrlWrapper } from "../decorators/index.js" ;
3
3
import User from "../models/Users.js" ;
4
4
import bcrypt from "bcrypt" ;
@@ -8,10 +8,11 @@ import gravatar from "gravatar";
8
8
import fs from "fs/promises" ;
9
9
import path from "path" ;
10
10
import Jimp from "jimp" ;
11
+ import { v4 as uuid4 } from "uuid" ;
11
12
12
13
const avatarPath = path . resolve ( "public" , "avatars" ) ;
13
14
14
- const { JWT_SECRET } = process . env ;
15
+ const { JWT_SECRET , BASE_URL } = process . env ;
15
16
16
17
const signUp = async ( req , res ) => {
17
18
const { password, email } = req . body ;
@@ -22,12 +23,23 @@ const signUp = async (req, res) => {
22
23
}
23
24
const hashPassword = await bcrypt . hash ( password , 10 ) ;
24
25
const avatarURL = gravatar . url ( email , { s : "200" , r : "pg" , d : "mm" } ) ;
26
+ const verificationCode = uuid4 ( ) ;
25
27
const newUser = await User . create ( {
26
28
email,
27
29
password : hashPassword ,
28
30
avatarURL,
31
+ verificationCode,
29
32
} ) ;
30
33
34
+ const verifyEmail = {
35
+ to : email ,
36
+ subject : "Verify Email" ,
37
+ html : `<a target="_blank" href="http://${ BASE_URL } /api/auth/${ verificationCode } ">Click to verify email</a>` ,
38
+ // html: `<a target="_blank" href="http://HOST/api/auth/${verificationCode}">Click to verify email</a>`,
39
+ } ;
40
+
41
+ await sendEmail ( verifyEmail ) ;
42
+
31
43
res . status ( 201 ) . json ( {
32
44
message :
33
45
"Registration successful. You can now sign in with your new account." ,
@@ -47,6 +59,10 @@ const signIn = async (req, res) => {
47
59
throw HttpError ( 401 , "Incorrect login or password" ) ;
48
60
}
49
61
62
+ if ( ! user . verify ) {
63
+ throw HttpError ( 401 , "Email not verify" ) ;
64
+ }
65
+
50
66
const passwordCompare = await bcrypt . compare ( password , user . password ) ;
51
67
if ( ! passwordCompare ) {
52
68
throw HttpError ( 401 , "Incorrect login or password" ) ;
@@ -118,11 +134,49 @@ const updateAvatar = async (req, res) => {
118
134
} ) ;
119
135
} ;
120
136
137
+ const verify = async ( req , res ) => {
138
+ const { verificationCode } = req . params ;
139
+ const user = await User . findOne ( { verificationCode } ) ;
140
+ if ( ! user ) {
141
+ throw HttpError ( 400 , "Email not found or already verified" ) ;
142
+ }
143
+
144
+ await User . findByIdAndUpdate ( user . _id , {
145
+ verify : true ,
146
+ verificationCode : "" ,
147
+ } ) ;
148
+ res . json ( { message : "Email verify success" } ) ;
149
+ } ;
150
+
151
+ const resendVerifyEmail = async ( req , res ) => {
152
+ const { email } = req . body ;
153
+ const user = await User . findOne ( { email } ) ;
154
+ if ( ! user ) {
155
+ throw HttpError ( 404 , "Email not found" ) ;
156
+ }
157
+ if ( user . verify ) {
158
+ throw HttpError ( 400 , "Email already verified" ) ;
159
+ }
160
+
161
+ const verifyEmail = {
162
+ to : email ,
163
+ subject : "Verify Email" ,
164
+ html : `<a target="_blank" href="http://${ BASE_URL } /api/auth/${ verificationCode } ">Click to verify email</a>` ,
165
+ // html: `<a target="_blank" href="http://HOST/api/auth/${verificationCode}">Click to verify email</a>`,
166
+ } ;
167
+
168
+ await sendEmail ( verifyEmail ) ;
169
+
170
+ res . json ( { message : "Verify email send success" } ) ;
171
+ } ;
172
+
121
173
export default {
122
174
signUp : ctrlWrapper ( signUp ) ,
123
175
signIn : ctrlWrapper ( signIn ) ,
124
176
getCurrent : ctrlWrapper ( getCurrent ) ,
125
177
logOut : ctrlWrapper ( logOut ) ,
126
178
updateSubscription : ctrlWrapper ( updateSubscription ) ,
127
179
updateAvatar : ctrlWrapper ( updateAvatar ) ,
180
+ verify : ctrlWrapper ( verify ) ,
181
+ resendVerifyEmail : ctrlWrapper ( resendVerifyEmail ) ,
128
182
} ;
0 commit comments