refactor: rename req.payload to req.authInfo to be less confusing
This commit is contained in:
parent
e607a1d64d
commit
761330e9b6
|
@ -15,7 +15,7 @@ function getTokenFromHeader(req) {
|
||||||
|
|
||||||
const jwtOptional = jwt({
|
const jwtOptional = jwt({
|
||||||
secret: secret,
|
secret: secret,
|
||||||
userProperty: 'payload',
|
userProperty: 'authInfo',
|
||||||
credentialsRequired: false,
|
credentialsRequired: false,
|
||||||
getToken: getTokenFromHeader,
|
getToken: getTokenFromHeader,
|
||||||
algorithms: ['HS256'],
|
algorithms: ['HS256'],
|
||||||
|
@ -27,19 +27,20 @@ async function getUserIdMiddleware(req, res, next) {
|
||||||
const [tokenType, token] = (authorization && authorization.split(' ')) || [];
|
const [tokenType, token] = (authorization && authorization.split(' ')) || [];
|
||||||
|
|
||||||
if (tokenType === 'Token' || tokenType === 'Bearer') {
|
if (tokenType === 'Token' || tokenType === 'Bearer') {
|
||||||
|
// only parse the token as jwt if it looks like one, otherwise we get an error
|
||||||
return jwtOptional(req, res, next);
|
return jwtOptional(req, res, next);
|
||||||
} else if (tokenType === 'OBSUserId') {
|
} else if (tokenType === 'OBSUserId') {
|
||||||
req.payload = { id: token.trim() };
|
req.authInfo = { id: token.trim() };
|
||||||
next();
|
next();
|
||||||
} else if (!authorization && req.body && req.body.id && req.body.id.length === 24) {
|
} else if (!authorization && req.body && req.body.id && req.body.id.length === 24) {
|
||||||
const user = await User.findById(req.body.id);
|
const user = await User.findById(req.body.id);
|
||||||
if (user) {
|
if (user) {
|
||||||
req.payload = { id: user.id };
|
req.authInfo = { id: user.id };
|
||||||
req.user = user;
|
req.user = user;
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
req.payload = null;
|
req.authInfo = null;
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -49,8 +50,8 @@ async function getUserIdMiddleware(req, res, next) {
|
||||||
|
|
||||||
async function loadUserMiddleware(req, res, next) {
|
async function loadUserMiddleware(req, res, next) {
|
||||||
try {
|
try {
|
||||||
if (req.payload && req.payload.id) {
|
if (req.authInfo && req.authInfo.id) {
|
||||||
req.user = await User.findById(req.payload.id);
|
req.user = await User.findById(req.authInfo.id);
|
||||||
|
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
return res.sendStatus(401);
|
return res.sendStatus(401);
|
||||||
|
@ -65,7 +66,7 @@ async function loadUserMiddleware(req, res, next) {
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
required(req, res, next) {
|
required(req, res, next) {
|
||||||
if (!req.payload) {
|
if (!req.authInfo) {
|
||||||
return res.sendStatus(403);
|
return res.sendStatus(403);
|
||||||
} else {
|
} else {
|
||||||
return next();
|
return next();
|
||||||
|
|
Loading…
Reference in a new issue