backend/src/middlewares/auth.middlewares.js
2023-04-07 18:06:47 +01:00

27 lines
869 B
JavaScript

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)
}
}