forked from MashaDordevic/Makao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
81 lines (70 loc) · 2.34 KB
/
routes.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
import express from 'express';
import jwt from 'jsonwebtoken';
import validations from './validations';
import Gameplay from './Gameplay/Gameplay';
import { loginAuth, signupAuth } from './auth-check';
var router = express.Router();
// POST login request
router.post('/auth/login', (req, res, next) => {
// first we validate the input fields
var validationResult = validations.validateLoginForm(req.body);
if (!validationResult.success) {
return res.status(400).json({
success: false,
message: validationResult.error
});
}
// if inputs are valid, we authenticate the user
// by sending him a JSON web token (jwt)
loginAuth(req.body, (err, user) => {
if (err) {
return res.status(400).json({
success: false,
message: err.message
});
}
var payload = { sub: user.id, name: user.username };
// create a JSON web token
var token = jwt.sign(payload, 'aips2017jajacmasamitic');
return res.status(200).json({
success: true,
message: "Successful login!",
token
});
})
});
// POST signup request
router.post('/auth/signup', (req, res, next) => {
// first we validate the input fields
var validationResult = validations.validateSignupForm(req.body);
if (!validationResult.success) {
return res.status(400).json({
success: false,
message: validationResult.error
});
}
// if inputs are valid, we register the user
// and send him a jwt
signupAuth(req.body, (err, user) => {
if (err) {
return res.status(400).json({
success: false,
message: err.message
});
}
var payload = { sub: user.id, name: user.username };
// create a JSON web token
var token = jwt.sign(payload, 'aips2017jajacmasamitic');
return res.status(200).json({
success: true,
message: "Successful signup!",
token
});
})
});
router.post('/game/watch', (req, res, next) => {
Gameplay.getGame(req.body.creatorUsername)
.then((state) => res.status(200).json({state: state}))
.catch((reason) => res.status(200).json({success: false, reason: reason}));
});
module.exports = router;