-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
96 lines (90 loc) · 3.27 KB
/
auth.js
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
const jwt = require("jsonwebtoken");
const { jwtConfig } = require("./config");
const { secret, expiresIn } = jwtConfig;
const { check } = require('express-validator');
const { User } = require('./db/models');
const getUserToken = (user) => {
const token = jwt.sign({ id: user.id, email: user.email }, secret, { expiresIn })
console.log(token)
return token;
}
const getUserFromToken = async (token) => {
try {
const payload = jwt.verify(
token,
secret
);
return await User.findByPk(payload.id);
} catch (err) {
return null;
}
}
const userLoginValidators = [
check('email')
.exists({ checkFalsy: true })
.withMessage('Please provide a value for Email Address')
.isLength({ max: 255 })
.withMessage('Email Address must not be more than 255 characters long')
.isEmail()
.withMessage('Email Address is not a valid email'),
check('password')
.exists({ checkFalsy: true })
.withMessage('Please provide a value for Password')
.isLength({ max: 50 })
.withMessage('Password must not be more than 50 characters long'),
];
const userCreatorValidators = [
check('firstName')
.exists({ checkFalsy: true })
.withMessage('Please provide a value for First Name')
.isLength({ max: 50 })
.withMessage('First Name must not be more than 50 characters long'),
check('lastName')
.exists({ checkFalsy: true })
.withMessage('Please provide a value for Last Name')
.isLength({ max: 50 })
.withMessage('Last Name must not be more than 50 characters long'),
check('email')
.exists({ checkFalsy: true })
.withMessage('Please provide a value for Email Address')
.isLength({ max: 255 })
.withMessage('Email Address must not be more than 255 characters long')
.isEmail()
.withMessage('Email Address is not a valid email')
.custom(value => {
return User.findOne({ where: { email: value } }).then(user => {
if (user) {
return Promise.reject('The provided Email Address is already in use by another account');
}
});
}),
check('phoneNumber')
.exists({ checkFalsy: true })
.withMessage('Please provide a value for Phone Number')
.isNumeric()
.withMessage("Must be numbers")
.isLength(10)
.withMessage('Password must be 10 digits long'),
check('password')
.exists({ checkFalsy: true })
.withMessage('Please provide a value for Password')
.isLength({ max: 50 })
.withMessage('Password must not be more than 50 characters long'),
check('confirmPassword')
.exists({ checkFalsy: true })
.withMessage('Please provide a value for Confirm Password')
.isLength({ max: 50 })
.withMessage('Confirm Password must not be more than 50 characters long')
.custom((value, { req }) => {
if (value !== req.body.password) {
throw new Error('Confirm Password does not match Password');
}
return true;
}),
];
module.exports = {
getUserToken,
getUserFromToken,
userCreatorValidators,
userLoginValidators,
};