nodejs-rigorous
Version:
Rigorous Framework
58 lines (37 loc) • 1.71 kB
JavaScript
const passport = require('passport');
module.exports = {
create: (req, res, next) => {
// A callback is called in JWTStrategy and something create authObjectData from cb(null, auth); populating it with ._doc
passport.authenticate('jwt',
{
session: false,
},
async (err, jwtPayload) => {
if (jwtPayload) {
req.authuser = jwtPayload;
next();
} else {
res.status(401).json({ response: errorTypes.RESPONSE_ERROR_TOKEN_AUTH, created_at: new Date() });
}
})(req, res, next);
},
refuseAnonymous: (req, res, next) => {
// A callback is called in JWTStrategy and something create authObjectData from cb(null, auth); populating it with ._doc
passport.authenticate('jwt',
{
session: false,
},
async (err, jwtPayloadObjectData) => {
if (jwtPayloadObjectData) {
const authuser = { ...jwtPayloadObjectData._doc };
if (authuser.role === 'anonymous') {
res.status(401).json({ response: errorTypes.RESPONSE_ERROR_NO_ANONYMOUS_AUTHORIZED, created_at: new Date() });
} else {
next();
}
} else {
res.status(401).json({ response: errorTypes.RESPONSE_ERROR_TOKEN_AUTH, created_at: new Date() });
}
})(req, res, next);
},
};