2020-11-20 10:02:30 +00:00
|
|
|
const jwt = require('express-jwt');
|
|
|
|
const secret = require('../config').secret;
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-09-29 20:08:29 +00:00
|
|
|
function getTokenFromHeader(req) {
|
2020-11-20 10:02:30 +00:00
|
|
|
if (
|
|
|
|
(req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Token') ||
|
|
|
|
(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;
|
|
|
|
}
|
|
|
|
|
2020-11-20 10:02:30 +00:00
|
|
|
const auth = {
|
2020-04-13 00:02:40 +00:00
|
|
|
required: jwt({
|
|
|
|
secret: secret,
|
|
|
|
userProperty: 'payload',
|
2020-09-29 20:08:29 +00:00
|
|
|
getToken: getTokenFromHeader,
|
2020-11-20 10:02:30 +00:00
|
|
|
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,
|
2020-11-20 10:02:30 +00:00
|
|
|
algorithms: ['HS256'],
|
|
|
|
}),
|
2020-04-13 00:02:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = auth;
|