token auth

This commit is contained in:
Erik 2022-03-26 14:16:02 +02:00
parent 404923a5ee
commit 7e8ac200b2
No known key found for this signature in database
GPG Key ID: FEFF4B220DDF5589

View File

@ -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;
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 };