-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJWT.js
More file actions
32 lines (26 loc) · 690 Bytes
/
JWT.js
File metadata and controls
32 lines (26 loc) · 690 Bytes
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
var jwt = require('jsonwebtoken');
var config = require('./config.js');
exports.getToken = function (user) {
return jwt.sign(user, config.secretKey, {
expiresIn: 1800000
});}
exports.verifyToken = function(req, res, next){
var token = req.query.token || req.headers['x-access-token'];
if (token){
jwt.verify(token, config.secretKey, function(err, decoded){
if(err){
var err = new Error('You are not authenticated');
res.status(401).end(err.message);
}
else {
req.decoded = decoded;
next();
}
});
}
else
{
var err = new Error("Not Authenticated");
res.status(401).end(err.message);
}
}