-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.route.js
258 lines (247 loc) · 11.4 KB
/
user.route.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'use strict';
const CONFIG = require('./app.config');
const UserController = require('./user.controller');
const privateMethods = {
initialize(app, passport, options, USER) {
const UserControllerInstance = new UserController(options, USER);
const user_router = require('express').Router(),
jwt = require('jsonwebtoken');
const session_secret_key = options && options.session_secret_key ? options.session_secret_key : 'secret';
const model_options = options && options.model_options ? options.model_options : {};
const email_filed_name = typeof model_options === 'object' && model_options.email_filed_name ? model_options.email_filed_name : 'email';
const password_filed_name = typeof model_options === 'object' && model_options.password_filed_name ? model_options.password_filed_name : 'password';
app.use(options && options.main_route ? options.main_route : '/user', user_router);
if (options && options.same_origin) {
user_router.route('/register').post(passport.authenticate('local-signup', { failureMessage: 'Invalid username or password' }), (req, res) => {
res.status(200).send({
msg: 'Successfully signup',
user: req.user,
success: true
});
});
user_router.route('/login').post(passport.authenticate('local-login', { failureMessage: 'Invalid username or password' }), (req, res) => {
res.status(200).send({
msg: 'Successfully login',
user: req.user,
success: true
});
});
user_router.route('/logout').post((req, res) => {
if (req.user) {
if (typeof req.session === 'function') {
return req.session.destroy(() => {
res.status(200).json({
msg: 'Successfully logout',
success: true
});
});
} else {
req.logout();
res.status(200).json({
msg: 'Successfully logout',
});
}
} else {
res.status(200).json({
msg: 'Successfully logout',
success: true
});
}
});
} else {
user_router.route('/login').post((req, res) => {
if (typeof req.body[email_filed_name] !== 'string' || typeof req.body[password_filed_name] !== 'string' || !req.body[email_filed_name].trim().length || !req.body[password_filed_name].trim().length)
return res.status(400).send({
msg: `Please provide ${email_filed_name} or ${password_filed_name}`,
success: false
});
passport.authenticate('local-login', { session: false }, (error, user) => {
if (error) {
return res.status(400).send({
msg: typeof error === 'string' ? error : 'Error by database',
success: false,
error: error
});
}
if (!user) {
return res.status(400).send({
msg: 'No user exist',
success: false
});
}
req.login(user, { session: false }, (error) => {
if (error) {
return res.status(400).send({
msg: 'Error by database',
success: false,
error: error
});
}
const token = jwt.sign({
id: user._id
}, session_secret_key);
return res.status(200).json({
token: token,
msg: 'Successfully login',
user: user,
success: true
});
});
})(req, res);
});
user_router.route('/register').post((req, res) => {
if (typeof req.body[email_filed_name] !== 'string' || typeof req.body[password_filed_name] !== 'string' || !req.body[email_filed_name].trim().length || !req.body[password_filed_name].trim().length)
return res.status(400).send({
msg: `Please provide ${email_filed_name} or ${password_filed_name}`,
success: false
});
req.body[email_filed_name] = req.body[email_filed_name].toLowerCase();
USER.findOne({ [email_filed_name]: req.body[email_filed_name] }).
exec((error, user) => {
if (error)
return res.status(400).send({
msg: 'Error by database',
success: false,
error: error
});
if (user)
return res.status(400).send({
msg: 'User already exist',
success: false,
});
var new_user = new USER(req.body);
new_user.generateHash(req.body[password_filed_name]);
new_user.save((error, user) => {
if (error)
return res.status(400).send({
msg: 'Error by database',
success: false,
error: error
});
else {
const token = jwt.sign({
id: user._id
}, 'user_maagement_key');
return res.status(201).json({
msg: 'Successfully signup',
user: user,
token: token,
success: true
});
}
});
});
});
user_router.route('/logout').post(passport.authenticate('jwt', { session: false }), (req, res) => {
if (req.user) {
if (typeof req.session === 'function') {
return req.session.destroy(() => {
res.status(200).json({
success: true,
msg: 'Successfully logout'
});
});
} else {
req.logout();
res.status(200).json({
success: true,
msg: 'Successfully logout'
});
}
} else {
res.status(200).json({
success: true,
msg: 'Successfully logout'
});
}
});
}
user_router.route('/').get((options && options.same_origin) ? this.isAuthenticate : passport.authenticate('jwt', { session: false }), (req, res) => {
UserControllerInstance.getUserDetail({
_id: req.user._id
})
.then((success) => {
res.status(200).send(success);
}, (error) => {
res.status(400).send(error);
});
});
user_router.route('/changePassword').put((options && options.same_origin) ? this.isAuthenticate : passport.authenticate('jwt', { session: false }), (req, res) => {
UserControllerInstance.changePassword(req.body.old_password, req.body.new_password, req.user.id)
.then((success) => {
res.status(200).send(success);
}, (error) => {
res.status(400).send(error);
});
});
user_router.route('/').put((options && options.same_origin) ? this.isAuthenticate : passport.authenticate('jwt', { session: false }), (req, res) => {
delete req.body[email_filed_name];
delete req.body[password_filed_name];
UserControllerInstance.editUserDetail(req.user._id, req.body)
.then((success) => {
res.status(200).send(success);
}, (error) => {
res.status(400).send(error);
});
});
user_router.route('/sendConfirmationMail/:email').get((req, res) => {
UserControllerInstance.sendConfirmationMail(req.params.email)
.then((success) => {
res.status(200).send(success);
}, (error) => {
res.status(400).send(error);
});
});
user_router.route('/verifyConfirmationToken/:token').get((req, res) => {
UserControllerInstance.verifyConfirmationToken(req.params.token)
.then((success) => {
res.status(200).send(success);
}, (error) => {
res.status(400).send(error);
});
});
user_router.route('/forgetPassword/:email').get((req, res) => {
UserControllerInstance.forgetPassword(req.params.email)
.then((success) => {
res.status(200).send(success);
}, (error) => {
res.status(400).send(error);
});
});
user_router.route('/verifyResetLink/:token').get((req, res) => {
UserControllerInstance.verifyResetLink(req.params.token)
.then((success) => {
res.status(200).send(success);
}, (error) => {
res.status(400).send(error);
});
});
user_router.route('/resetPassword').post((req, res) => {
UserControllerInstance.resetPassword(req.body.token, req.body[password_filed_name])
.then((success) => {
res.status(200).send(success);
}, (error) => {
res.status(400).send(error);
});
});
}
};
class UserRoute {
constructor(app, passport, options, USER) {
privateMethods.initialize.call(this,app, passport, options, USER);
}
isAuthenticate(req, res, done) {
if (req.user) {
req.session._garbage = Date();
req.session.touch();
req.session.cookie.expires = CONFIG.EXPRESS_SESSION.COKKIES_MAX_AGE;
done();
} else {
res.status(401).send({
message: 'unauthorized',
success: false
});
}
}
}
module.exports = UserRoute;