2020-04-13 00:02:40 +00:00
|
|
|
var jwt = require('express-jwt');
|
|
|
|
var secret = require('../config').secret;
|
|
|
|
|
2020-09-29 20:08:29 +00:00
|
|
|
function getTokenFromHeader(req) {
|
2020-04-13 00:02:40 +00:00
|
|
|
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Token' ||
|
2020-09-29 20:08:29 +00:00
|
|
|
req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
|
2020-04-13 00:02:40 +00:00
|
|
|
return req.headers.authorization.split(' ')[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var auth = {
|
|
|
|
required: jwt({
|
|
|
|
secret: secret,
|
|
|
|
userProperty: 'payload',
|
2020-09-29 20:08:29 +00:00
|
|
|
getToken: getTokenFromHeader,
|
|
|
|
algorithms: ['HS256']
|
2020-04-13 00:02:40 +00:00
|
|
|
}),
|
|
|
|
optional: jwt({
|
|
|
|
secret: secret,
|
|
|
|
userProperty: 'payload',
|
|
|
|
credentialsRequired: false,
|
2020-09-29 20:08:29 +00:00
|
|
|
getToken: getTokenFromHeader,
|
|
|
|
algorithms: ['HS256']
|
2020-04-13 00:02:40 +00:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = auth;
|