backend/src/middlewares/auth.middlewares.js

28 lines
806 B
JavaScript
Raw Normal View History

const utils = require('../utils/auth.utils');
const log = require('../utils/log.utils');
module.exports = async function authCheck(req, res, next) {
log.out('authMiddlewares: Checking authentication', 'dbug');
try {
var uuid = req.headers.uuid;
} catch(err) {
log.out('authMiddlewares: No authentication attempted', 'dbug');
err.status = 401;
return next(err);
}
try {
var result = await utils.isAuthed(uuid) || false;
if (!result) {
const err = new Error('Unauthorised');
err.status = 401;
log.out('authMiddlewares: Authentication attempted with incorrect key',
'warn');
return next(err);
} else {
log.out('authMiddlewares: User authenticated', 'dbug');
return next();
}
} catch(err) {
return next(err);
}
};