add helper methods for authorization to Authenticator

This commit is contained in:
Markus Schubert 2020-03-26 00:06:24 +01:00
parent fc3852b3cb
commit d2507c9770

View file

@ -25,6 +25,20 @@ class Authenticator {
} }
}; };
withUser(req, res, callback) {
this.getAuthenticatedUser(req, (user) => {
if (user) callback(user);
else res.status(401).send({ code: 'auth error', message: 'user not logged in.' });
});
};
withAdmin(req, res, callback) {
this.withUser(req, res, (user) => {
if (user.role === 'Admin') callback(user)
else res.status(403).send({ code: 'auth error', message: 'user is not Admin.' });
});
};
}; };
module.exports = Authenticator; module.exports = Authenticator;