obs-portal/routes/auth.js

32 lines
724 B
JavaScript
Raw Normal View History

2020-11-20 10:02:30 +00:00
const jwt = require('express-jwt');
const secret = require('../config').secret;
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')
) {
return req.headers.authorization.split(' ')[1];
}
return null;
}
2020-11-20 10:02:30 +00:00
const auth = {
required: jwt({
secret: secret,
userProperty: 'payload',
getToken: getTokenFromHeader,
2020-11-20 10:02:30 +00:00
algorithms: ['HS256'],
}),
optional: jwt({
secret: secret,
userProperty: 'payload',
credentialsRequired: false,
getToken: getTokenFromHeader,
2020-11-20 10:02:30 +00:00
algorithms: ['HS256'],
}),
};
module.exports = auth;