From d2507c9770aa7bf52b56a8a6fe11c18068f18485 Mon Sep 17 00:00:00 2001 From: Markus Schubert Date: Thu, 26 Mar 2020 00:06:24 +0100 Subject: [PATCH] add helper methods for authorization to Authenticator --- app/Authenticator.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/Authenticator.js b/app/Authenticator.js index 4a0220d..d97599a 100644 --- a/app/Authenticator.js +++ b/app/Authenticator.js @@ -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;