-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
154 lines (93 loc) · 4 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
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
'use strict';
const auth = require('basic-auth');
const jwt = require('jsonwebtoken');
const register = require('./functions/register');
const login = require('./functions/login');
const profile = require('./functions/profile');
const password = require('./functions/password');
const config = require('./config/config.json');
module.exports = router => {
router.get('/demo/', (req, res) => res.end('Welcome to Learn2Crack !'));
router.post('/authenticate', (req, res) => {
const credentials = auth(req);
if (!credentials) {
res.status(400).json({message: 'Invalid Request !'});
} else {
login.loginUser(credentials.name, credentials.pass)
.then(result => {
const token = jwt.sign(result, config.secret, {expiresIn: 1440});
res.status(result.status).json({message: result.message, token: token});
})
.catch(err => res.status(err.status).json({message: err.message}));
}
});
router.post('/users', (req, res) => {
console.log(req.body)
const name = req.body.name;
const email = req.body.email;
const password = req.body.password;
console.log(email)
if (!name || !email || !password || !name.trim() || !email.trim() || !password.trim()) {
res.status(400).json({message: 'Invalid Request !'});
} else {
register.registerUser(name, email, password)
.then(result => {
res.setHeader('Location', '/users/' + email);
res.status(result.status).json({message: result.message})
})
.catch(err => res.status(err.status).json({message: err.message}));
}
});
router.get('/users/:id', (req, res) => {
console.log(req)
if (checkToken(req)) {
profile.getProfile(req.params.id)
.then(result => res.json(result))
.catch(err => res.status(err.status).json({message: err.message}));
} else {
res.status(401).json({message: 'Invalid Token !'});
}
});
router.put('/users/:id', (req, res) => {
if (checkToken(req)) {
const oldPassword = req.body.password;
const newPassword = req.body.newPassword;
if (!oldPassword || !newPassword || !oldPassword.trim() || !newPassword.trim()) {
res.status(400).json({message: 'Invalid Request !'});
} else {
password.changePassword(req.params.id, oldPassword, newPassword)
.then(result => res.status(result.status).json({message: result.message}))
.catch(err => res.status(err.status).json({message: err.message}));
}
} else {
res.status(401).json({message: 'Invalid Token !'});
}
});
router.post('/users/:id/password', (req, res) => {
const email = req.params.id;
const token = req.body.token;
const newPassword = req.body.password;
if (!token || !newPassword || !token.trim() || !newPassword.trim()) {
password.resetPasswordInit(email)
.then(result => res.status(result.status).json({message: result.message}))
.catch(err => res.status(err.status).json({message: err.message}));
} else {
password.resetPasswordFinish(email, token, newPassword)
.then(result => res.status(result.status).json({message: result.message}))
.catch(err => res.status(err.status).json({message: err.message}));
}
});
function checkToken(req) {
const token = req.headers['x-access-token'];
if (token) {
try {
var decoded = jwt.verify(token, config.secret);
return decoded.message === req.params.id;
} catch (err) {
return false;
}
} else {
return false;
}
}
}