-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·496 lines (469 loc) · 13.4 KB
/
server.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/**
*
* Simple Restful API server with JSON Web Token authentication
*
*/
// ------------------------------------------------------------
// Include Dependencies
const source = 'db.json';
const jsonServer = require('json-server');
const fs = require('fs-extra');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('db.json');
const db = low(adapter);
const jwt = require('jsonwebtoken');
const shortid = require('shortid');
// Config Constants
const SECRET_KEY = 'someapisecrethere';
const EXPIRES_IN = 86400; // 24h
const HTTP_STATUS = {
Ok: 200,
Created: 201,
BadRequest: 400,
Unauthorized: 401,
Forbidden: 403,
NotFound: 404,
MethodNotAllowed: 405,
UnprocessableEntity: 422,
};
// Json server setup
const app = jsonServer.create();
const router = jsonServer.router(source);
// App middlewares
const middlewares = [
jsonServer.defaults({ noCors: true }),
[
(req, res, next) =>
fs.readJson(source).then(contents => {
router.db.assign(contents).write();
next();
}),
],
];
app.use(middlewares);
// Using JSON Server bodyParser for HTTP methods
app.use(jsonServer.bodyParser);
// Allow API endpoints
let allowEnpoints = [
'/db',
'/api/register',
'/api/signup',
'/api/token-refresh',
'/api/password-reset',
'/api/password-reset-confirm',
];
/*=============================================
= Application Interceptor =
=============================================*/
// Avoid CORS issue
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.header(
'Access-Control-Allow-Headers',
'Content-Type, Authorization, Content-Length, X-Requested-With'
);
//intercepts OPTIONS method
if ('OPTIONS' === req.method) {
//respond with 200
res.send(200);
}
next();
});
app.use((req, res, next) => {
// Check permited Endpoints
if (
allowEnpoints.indexOf(req.url) >= 0 ||
req.url.substring(0, 6) == '/apidoc'
) {
return next();
}
// Get Token from request headers
let token = req.headers['authorization'];
if (!token) {
res
.status(HTTP_STATUS.Unauthorized)
.send({ error: 'Sorry, missing or malformed token' });
} else {
// Clean token string
token = token.replace('Bearer ', '');
// Validate token
jwt.verify(token, SECRET_KEY, function(error, decoded) {
if (error) {
return res
.status(HTTP_STATUS.Unauthorized)
.send({ auth: false, error: 'Sorry invalid token' });
} else {
// Hold current user
req.userId = decoded.id;
console.log(req.userId);
}
});
// Go to JSON Server db routers
next();
}
});
/*===== End of Application Interceptor ======*/
/*=============================================
= TOKEN GENERATOR =
=============================================*/
/**
* @api {post} /api/token-refresh Refresh User Token
* @apiDescription Refresh user token using the current one. Tokens are valid for 24hours.
* @apiHeader {String} authorization Users unique access-token.
* @apiHeaderExample {json} Header-Example:
* {
* "Authorization": "Bearer <token>"
* }
* @apiGroup Token
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "auth": true,
* "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
* "user": {
* "email": "johnny@cash.com",
* "username": "Johnny Cash",
* "password": "123456",
* "type": "Customer",
* "id": 1
* }
* }
*
* @apiError UnprocessableEntity The server understands the content type of the request entity but was unable to process the contained instructions.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 422 Unprocessable Entity
* {
* "auth": "false",
* "error": "Refresh Token failed"
* }
*/
app.get('/api/token-refresh', (req, res) => {
// Get Token from Request Header
let token = req.headers['authorization'];
token = token.replace('Bearer ', '');
// Check Token
jwt.verify(token, SECRET_KEY, function(error, decoded) {
if (error) {
return res
.status(HTTP_STATUS.UnprocessableEntity)
.send({ auth: false, error: 'Refresh Token failed' });
}
// Generate new Token
let token = jwt.sign({ id: decoded.id }, SECRET_KEY, {
expiresIn: EXPIRES_IN,
});
// Get User
let user = db
.get('users')
.find({ id: decoded.id })
.value();
// Return a User Object with new generated token
res.status(200).send({
auth: true,
token: token,
user: user,
});
});
});
/*========== End of TOKEN GENERATOR ========*/
/*=============================================
= User Register and Login =
=============================================*/
/**
* @api {post} /api/register Register
* @apiDescription Create a new User.
* @apiGroup User
*
* @apiParam {String} username User name
* @apiParam {String} email User email-address
* @apiParam {String} password User password.
* @apiParam {String} type User profile type. Possible: Admin or Customer
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 201 Created
* {
* "username":"Jesse James"
* "email": "james@jesse.com",
* "password": "123456",
* "type": "Customer",
* "id": 1
* }
*
* @apiError UnprocessableEntity The server understands the content type of the request entity but was unable to process the contained instructions.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 422 Unprocessable Entity
* {
* "error": "Missing mandatory fields"
* }
*/
app.post('/api/register', (req, res) => {
// Get new password from request body
const email = req.body.email;
const username = req.body.username;
const password = req.body.password;
const type = req.body.type;
// Check For Missing fields
if (!email || !username || !password || !type) {
return res
.status(HTTP_STATUS.UnprocessableEntity)
.send({ error: 'Missing one or more mantatory fields' });
}
// Create a new user object
const newUser = {
id: shortid.generate(),
email: email,
username: username,
password: password,
type: type,
};
// Set new user
db.get('users')
.push(newUser)
.write();
res.status(HTTP_STATUS.Created).send(newUser);
});
/**
* @api {post} /api/signup Signup
* @apiDescription User must exist on database
* @apiGroup User
*
* @apiParam {String} email User email address.
* @apiParam {String} password User password.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "auth": true,
* "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
* "user": {
* "email": "johnny@cash.com",
* "username": "Johnny Cash",
* "password": "123456",
* "type": "Customer",
* "id": 1
* }
* }
*
* @apiError BadRequest The request could not be understood by the server due to malformed syntax.
* @apiError NotFound The server has not found anything matching the Request-URI.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 400 Bad Request
* {
* "error": "Sorry, email or password not provide"
* }
* @apiErrorExample Error-Response:
* HTTP/1.1 400 Bad Request
* {
* "error": "Sorry, invalid combination of email and password"
* }
*
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "error": "You don't have an account yet"
* }
*/
app.post('/api/signup', (req, res) => {
// Get payload request
const payload = req.body;
// Check for empty fields
if (!payload.email || !payload.password) {
return res
.status(HTTP_STATUS.BadRequest)
.send({ error: 'Sorry, email or password not provide' });
}
// Get user from db
let user = db
.get('users')
.filter(user => user.email === payload.email)
.value();
// Check if User exist on db
if (user.length === 0) {
return res
.status(HTTP_STATUS.NotFound)
.send({ error: "You don't have an account yet" });
}
// Set user
user = user[0];
// Check user password
if (user.password !== payload.password) {
return res
.status(HTTP_STATUS.BadRequest)
.send({ error: 'Sorry, invalid combination of email and password' });
}
// Generate a Token
const token = jwt.sign({ id: user.id }, SECRET_KEY, {
expiresIn: EXPIRES_IN,
});
// Return a User Object with generated token
res.status(200).send({
auth: true,
token: token,
user: user,
});
});
/*===== End of User Register and Login ======*/
/*=============================================
= User Profile Actions =
=============================================*/
/**
* @api {post} /users/change-password Change Password
* @apiDescription Only logged users can change their password.
* @apiGroup Profile
*
* @apiParam {String} password User current password.
* @apiParam {String} new_password User new password.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "Your password has changed"
* }
*
* @apiError BadRequest The request could not be understood by the server due to malformed syntax.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 400 Bad Request
* {
* "error": "Your current password is invalid"
* }
*/
app.post('/users/change-password', (req, res) => {
// Get current password from Request body
let password = req.body.password;
// Get new password from request body
let new_password = req.body.new_password;
// Get authenticated User from db
let user = db
.get('users')
.find({ id: req.userId })
.value();
// Check passoword
if (user.password !== password)
return res
.status(HTTP_STATUS.BadRequest)
.send({ error: 'Your current password is invalid' });
// Store the new Password
db.get('users')
.find({ id: req.userId })
.assign({ password: new_password })
.write()
.then(() => {
console.log('Password changed');
});
res.status(HTTP_STATUS.Ok).send('Your password has changed');
});
/*=========== End of User Profile Actions =========*/
/*=============================================
= API Settings =
=============================================*/
/**
* @api {post} /api/password-reset Reset Password
* @apiDescription Reset password for non logged users.
* @apiGroup Password
*
* @apiParam {String} email User email.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "token": "<temporary token>"
* }
*
* @apiError UnprocessableEntity The server understands the content type of the request entity but was unable to process the contained instructions.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 422 Unprocessable Entity
* {
* "error": "You don't have an account yet"
* }
*/
app.post('/api/password-reset', (req, res) => {
// Get email from request body
let email = req.body.email;
// Get User from db
var user = db
.get('users')
.filter(user => user.email === email)
.value();
// Check User
if (user.length == 0) {
return res
.status(HTTP_STATUS.NotFound)
.send({ error: "You don't have an account yet" });
}
// Set User
user = user[0];
// Generate a temporary Token
const token = jwt.sign({ id: user.id }, SECRET_KEY, {
expiresIn: EXPIRES_IN,
});
// Save new generated Token on password resets table
db.get('password_reset_tokens')
.push({
user_id: user.id,
token: token,
})
.write();
res.status(200).send({ token: token });
});
/**
* @api {post} /api/password-reset-confirm Reset Password Confirm
* @apiDescription Confirm Reset password using temporary token and new password.
* @apiGroup Password
*
* @apiParam {String} token Temporary token generated on Reset password.
* @apiParam {String} password User new password
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "Your password was reseted"
* }
*
* @apiError UnprocessableEntity The server understands the content type of the request entity but was unable to process the contained instructions.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 422 Unprocessable Entity
* {
* "error": "Missing or invalid temporary Token"
* }
*/
app.post('/api/password-reset-confirm', (req, res) => {
// Get new password from request body
let new_password = req.body.new_password;
let token = db
.get('password_reset_tokens')
.find({ token: req.body.token })
.value();
// Check Token
if (!token) {
return res
.status(HTTP_STATUS.UnprocessableEntity)
.send({ error: 'Missing or invalid temporary Token' });
}
// Get user and temporary Token
db.get('users')
.find({ id: token.user_id })
.assign({ password: new_password })
.write()
.then(() => {
db.get('password_reset_tokens')
.remove({ user_id: token.user_id })
.write();
});
res.status(HTTP_STATUS.Ok).send('Your password was reseted');
});
/*============= End of API Settings ========*/
// Start server
app.use('/api', router);
app.set('port', process.env.PORT || 3000);
server = app.listen(app.get('port'), () => {
console.log('Application running on port: ' + server.address().port);
});