-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjwt_auth.js
38 lines (31 loc) · 903 Bytes
/
jwt_auth.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
var models = require('./models');
var jwt = require ('jsonwebtoken');
var sequelize = require('sequelize');
var User = models.User;
module.exports = function(req, res, next) {
// console.log("Header: " + JSON.stringify(req.headers));
// console.log("Body: " + JSON.stringify(req.body));
// console.log("Header authorization: " + req.headers['authorization']);
if (req.method == 'OPTIONS') {
//console.log("OPTIONS REQUEST FOUND.")
res.status(200).end();
} else {
// allows three ways to insert token into the request
var token = (req.body && req.body.access_token)
|| (req.query && req.query.access_token)
|| req.headers['authorization'];
if (token) {
jwt.verify(token, 'ashu1234', function(err, decoded) {
if (err) {
res.sendStatus(401);
}
else {
req.decoded = decoded;
next();
}
});
} else {
res.sendStatus(403);
}
}
}