diff --git a/server/src/client/middleware/Auth.js b/server/src/client/middleware/Auth.js index 2cfd767..de3b468 100644 --- a/server/src/client/middleware/Auth.js +++ b/server/src/client/middleware/Auth.js @@ -1,7 +1,32 @@ -const CheckAuth = (req, res, next) => { +const msg = { message: 'Must be authenticated for this endpoint' }; + +const CheckSession = (req, res, next) => { res.set('Access-Control-Allow-Credentials', true); if (req.isAuthenticated()) return next(); - return res.status(401).json({ message: 'Must be authenticated for this endpoint' }); + return res.status(401).json(msg); }; -module.exports = CheckAuth; \ No newline at end of file +const CheckSessionOrToken = async (req, res, next) => { + + res.set('Access-Control-Allow-Credentials', true); + if (req.isAuthenticated()) return next(); + + const token = req.get('Authorization') || null; + const user = token ? await req.client.users.checkToken(token) : null; + if (user) { + req.user = user; + return next(); + } + + return res.status(401).json(msg); + +}; + +const CheckToken = async (req, res, next) => { + const token = req.get('Authorization') || null; + const result = token ? await req.client.users.checkToken(token) : false; + if (result) return next(); + return res.status(401).json(msg); +}; + +module.exports = { CheckSession, CheckSessionOrToken, CheckToken }; \ No newline at end of file