-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (129 loc) · 4.67 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const express = require('express');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
/**
* @returns {ExpressRouter}
* @param {MongooseModel} User
* @param {string} token_secret
*/
module.exports = function (User, token_secret) {
const router = express.Router();
router.post('/register', async (req, res) => {
const user = { ...req.body };
const userFounded = await User.find({ email: user.email });
if (userFounded.length > 0) {
res.status(401).json({
success: false,
message: 'User already exists!'
});
} else {
const hash = await bcrypt.hash(user.password, 3);
user.password = hash;
const newUser = new User({ ...user });
await newUser.save();
const accessToken = await jwt.sign({ id: newUser.id, email: newUser.email }, token_secret);
res.status(201).json({
success: true,
data: {
accessToken
}
});
}
});
router.get('/remember-me/:token', async (req, res) => {
const token = req.params.token;
let decodedToken;
try {
decodedToken = await jwt.verify(token, token_secret);
res.status(200).json({ message: 'Token verified!', statusText: 'ok', success: true });
} catch (e) {
decodedToken = await jwt.decode(token);
console.log(decodedToken);
const foundedUser = await User.find({ email: decodedToken.email });
if (foundedUser.length > 0) {
const accessToken = jwt.sign({ token: foundedUser[0].refreshToken }, token_secret);
res.status(200).json({
message: 'Token verified!',
statusText: 'ok',
data: { accessToken },
success: true
});
} else {
res.status(401).json({
success: false,
message: 'User not found!',
statusText: 'failed'
});
}
}
});
router.post('/login', async (req, res) => {
const { email, password, rm } = req.body;
const foundedUser = await User.find({ email });
if (foundedUser.length === 0) {
res.status(401).json({ success: false, message: 'Invalid credentials!' });
} else {
const [user] = foundedUser;
const verifyStatus = await bcrypt.compare(password, user.password);
if (!verifyStatus) {
res.status(401).json({ success: false, message: 'Invalid credentials!' });
} else {
const accessToken = jwt.sign({ id: user.id, email: user.email }, token_secret);
if (rm) {
const refreshToken = jwt.sign({ id: user.id, email: user.email }, token_secret);
user.refreshToken = refreshToken;
await user.save();
}
res.status(200).json({
success: true,
data: {
accessToken
}
});
}
}
});
router.post('/reset-password-link', async (req, res) => {
const { email, baseUrl } = req.body;
const foundedUser = await User.find({ email });
if (foundedUser.length === 0) {
res.status(401).json({ success: false, message: 'Invalid email address!' });
} else {
const token = await jwt.sign({
data: foundedUser[0].id
}, token_secret, {
expiresIn: 600
});
res.status(200).json({
success: true,
data: {
token,
url: baseUrl ? baseUrl + token : token,
}
});
}
});
router.post('/change-password/:token', async (req, res) => {
const token = req.params.token;
const password = req.body.password;
try {
const id = (await jwt.verify(token, token_secret)).data;
const hash = await bcrypt.hash(password, 3);
console.log(id);
const user = await User.findById(id);
console.log(user);
user.password = hash;
await user.save();
res.status(200).json({
success: true,
message: 'Password changed!'
})
} catch (e) {
res.status(400).json({
success: false,
message: e.message
})
}
});
return router;
}