forked from thatkazuk1/QuickCredit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateUser.js
More file actions
106 lines (101 loc) · 3.31 KB
/
Copy pathvalidateUser.js
File metadata and controls
106 lines (101 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import HelperUtils from '../utils/helperUtils';
import DB from '../database/dbconnection';
/**
* @class ValidateUser
* @description Intercepts and validates a given request for User endpoints
* @exports ValidateUser
*/
export default class ValidateUser {
/**
* @method validateProfileDetails
* @description Validates profile details of user upon registration
* @param {object} req - The Request Object
* @param {object} res - The Response Object
* @returns
*/
static validateProfileDetails(req, res, next) {
req
.checkBody('firstname').notEmpty()
.withMessage('First name is required')
.trim()
.isLength({ min: 3, max: 15 })
.withMessage('First name should be between 3 to 15 charcters')
.isAlpha()
.withMessage('First name should only contain alphabets');
req
.checkBody('lastname').notEmpty()
.withMessage('Last name is required')
.trim()
.isLength({ min: 3, max: 15 })
.withMessage('Last name should be between 3 to 15 charcters')
.isAlpha()
.withMessage('Last name should only contain alphabets');
req
.checkBody('address').notEmpty()
.withMessage('Address field is required')
.trim()
.isLength({ min: 10, max: 50 })
.withMessage('Address should be between 10 to 50 characters')
// eslint-disable-next-line no-useless-escape
.matches(/^[A-Za-z0-9\.\-\s\,]*$/)
.withMessage('Invalid address format entered');
req
.checkBody('email').notEmpty()
.withMessage('Email field is required')
.trim()
.isEmail()
.withMessage('Invalid email address entered')
.customSanitizer(email => email.toLowerCase());
req
.checkBody('password').notEmpty()
.withMessage('Password is required')
.trim()
.isLength({ min: 6, max: 15 })
.withMessage('Password must be between 6 to 15 characters');
const errors = req.validationErrors();
if (errors) {
res.status(400).json({ status: 400, error: errors[0].msg });
return;
}
next();
}
/**
* @method validateLoginDetails
* @description Validates login details (email and password)
* @param {object} req - The Request Object
* @param {object} res - The Response Object
* @returns
*/
static async validateLoginDetails(req, res, next) {
req
.checkBody('email').notEmpty()
.withMessage('Email field is required')
.trim()
.isEmail()
.withMessage('Invalid email address entered')
.customSanitizer(email => email.toLowerCase());
req
.checkBody('password').notEmpty()
.withMessage('Password field is required');
const errors = req.validationErrors();
if (errors) {
res.status(400).json({ error: errors[0].msg });
return;
}
const query = `SELECT * from users WHERE email='${req.body.email}'`;
const { rows, rowCount } = await DB.query(query);
if (rowCount < 1) {
res.status(401).json({ error: 'Email/Password is incorrect' });
return;
}
const hashedPassword = rows[0].password;
const verifyPassword = HelperUtils.verifyPassword(`${req.body.password}`, hashedPassword);
if (!verifyPassword) {
res.status(401).json({ error: 'Email/Password is incorrect' });
return;
}
const userReq = rows[0];
req.user = userReq;
next();
}
}