1
1
import bcrypt from 'bcrypt' ;
2
2
import jwt from 'jsonwebtoken' ;
3
3
import localStorage from 'localStorage' ;
4
- import env from 'dotenv' ;
5
- import sgMail from '@sendgrid/mail' ;
6
- import sequelize from 'sequelize' ;
4
+ import environment from 'dotenv' ;
5
+ import { verifyAccount , verifyPassword } from '../helpers/emailBody' ;
7
6
import models from '../db/models' ;
8
7
import generateToken from '../helpers/generateToken' ;
9
8
import generatePswd from '../helpers/randomPswd' ;
10
9
import usePasswordHashToMakeToken from '../helpers/helpers' ;
11
- import { getPasswordResetURL , resetPasswordTemplate } from '../modules/email' ;
10
+ import { getPasswordResetURL , resetPasswordTemplate , getEmailVerifytURL } from '../modules/email' ;
12
11
import userQuery from '../helpers/userQueries' ;
13
12
import handleError from '../helpers/errorHandler' ;
14
13
import handleRedirects from '../helpers/handleRedirects' ;
15
14
16
- const { Op } = sequelize ;
17
- env . config ( ) ;
15
+ environment . config ( ) ;
18
16
19
17
export default class usersController {
20
18
static async registerUser ( req , res ) {
21
19
try {
22
20
const {
23
21
firstName, lastName, gender, email, password,
24
22
} = req . body ;
23
+ const existingUser = await models . User . findOne ( { where : { email } } ) ;
24
+ if ( existingUser !== null ) return res . status ( 409 ) . json ( { error : 'Email has already taken.' } ) ;
25
25
const token = generateToken ( {
26
26
firstName, lastName, gender, email, password,
27
27
} ) ;
28
- let host ;
29
- const userExists = await models . User . findOne ( { where : { email } } ) ;
30
- if ( userExists ) return res . status ( 409 ) . json ( { error : 'Email already exists.' } ) ;
31
-
32
- process . env . NODE_ENV === 'development' ? host = process . env . LOCAL_HOST : host = process . env . HOST_NAME ;
33
- const url = `${ host } /api/v1/auth/signup/${ token } ` ;
34
- sgMail . setApiKey ( process . env . BN_API_KEY ) ;
35
- const msg = {
36
- to : email ,
37
-
38
- subject : 'Account Verification' ,
39
- html : `<strong> Dear ${ firstName } , please open this <a href="${ url } ">link</a> to verify your account </strong>` ,
28
+ const user = {
29
+ lastName,
30
+ email,
40
31
} ;
41
- sgMail . send ( msg ) ;
42
- return res . status ( 200 ) . json ( { message : 'Please go to your email address to verify your account.' } ) ;
32
+ const url = getEmailVerifytURL ( token ) ;
33
+ resetPasswordTemplate ( user , url , verifyAccount ) ;
34
+ return res . status ( 200 ) . json ( { message : 'Check a verification link in your email .' } ) ;
43
35
} catch ( error ) {
44
- return res . status ( 500 ) . json ( { Error : error . message } ) ;
36
+ return res . status ( 500 ) . json ( { error : error . message } ) ;
45
37
}
46
38
}
47
39
@@ -52,12 +44,10 @@ export default class usersController {
52
44
const {
53
45
firstName, lastName, gender, email, password,
54
46
} = userInfo ;
47
+ const existingUser = await models . User . findOne ( { where : { email } } ) ;
48
+ if ( existingUser !== null ) return res . status ( 409 ) . json ( { error : 'Email has already taken.' } ) ;
55
49
const salt = await bcrypt . genSalt ( 10 ) ;
56
50
const hashedPassword = await bcrypt . hash ( password , salt ) ;
57
- const existingUser = await models . User . findOne ( {
58
- where : { [ Op . or ] : [ { email } ] } ,
59
- } ) ;
60
- if ( existingUser !== null ) return handleRedirects ( res , process . env . EMAIL_VERIFICATION_REDIRECT_LINK , { message : 'Email already exists.' } ) ;
61
51
const newUser = await models . User . create ( {
62
52
firstName, lastName, gender, email, password : hashedPassword ,
63
53
} ) ;
@@ -78,7 +68,7 @@ export default class usersController {
78
68
if ( existUser === null ) throw 'Seems you do not have an account! Create it now' ;
79
69
const passwordMatch = await bcrypt . compare ( password , existUser . password ) ;
80
70
if ( ! passwordMatch ) {
81
- return res . status ( 401 ) . json ( { status : 401 , message : 'Invalid credentials' } ) ;
71
+ return res . status ( 401 ) . json ( { status : 401 , error : 'Invalid credentials' } ) ;
82
72
}
83
73
const {
84
74
id, role, firstName, lastName,
@@ -140,15 +130,15 @@ export default class usersController {
140
130
const user = await models . User . findOne ( { where : { email } } ) ;
141
131
if ( ! user ) {
142
132
res . status ( 404 ) . json ( {
143
- error : 'email is not registered! Please check the entered email ' ,
133
+ error : 'email is not registered' ,
144
134
} ) ;
145
135
} else {
146
136
const token = usePasswordHashToMakeToken ( user ) ;
147
137
const url = getPasswordResetURL ( user , token ) ;
148
138
try {
149
- resetPasswordTemplate ( user , url ) ;
139
+ resetPasswordTemplate ( user , url , verifyPassword ) ;
150
140
return res . status ( 200 ) . json ( {
151
- message : `verify throughout your email: ${ user . email } before 1 hour` ,
141
+ message : 'Check in your email a link for changing password' ,
152
142
} ) ;
153
143
} catch ( error ) {
154
144
res . status ( 500 ) . json ( { error : 'error sending email' } ) ;
@@ -186,9 +176,9 @@ export default class usersController {
186
176
const { email } = req . query ;
187
177
const { role } = req . body ;
188
178
try {
189
- if ( req . user . role !== 'superAdmin' ) return res . status ( 403 ) . json ( { status : 403 , message : 'Sorry! Only super admin authorized!' } ) ;
179
+ if ( req . user . role !== 'superAdmin' ) return res . status ( 403 ) . json ( { status : 403 , error : 'Sorry! Only super admin authorized!' } ) ;
190
180
const existingUser = await userQuery . getUserByEmail ( email ) ;
191
- if ( ! existingUser ) return res . status ( 404 ) . json ( { status : 404 , message : `User ${ email } is not found!` } ) ;
181
+ if ( ! existingUser ) return res . status ( 404 ) . json ( { status : 404 , error : `User ${ email } is not found!` } ) ;
192
182
await userQuery . updateUserRole ( role , email ) ;
193
183
return res . status ( 200 ) . json ( { status : 200 , message : 'User successfully updated!' } ) ;
194
184
} catch ( error ) {
0 commit comments