-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathverify.js
More file actions
39 lines (36 loc) · 1.21 KB
/
verify.js
File metadata and controls
39 lines (36 loc) · 1.21 KB
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
var User = require('./model/User.model');
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var config = require('./config/config.js')();
exports.getToken = function (user) {
return jwt.sign(user, config.secret, {
expiresIn: config.expiresIn || 3600
});
};
exports.verifyOrdinaryUser = function (req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
if(req.method==="OPTIONS")
{
next();
}
else if (token) {
// verifies secret and checks exp
jwt.verify(token, config.secret, function (err, decoded) {
if (err) {
var err = new Error('You are not authenticated!');
err.status = 401;
return next(err);
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error
var err = new Error('No token provided!');
err.status = 403;
return next(err);
}
};