Tidy auth middleware

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2024-03-10 21:01:00 +00:00
parent 236d85648d
commit 87532b001d

View File

@ -1,46 +1,45 @@
import { NextFunction, Request, Response } from "express";
const utils = require("../utils/auth.utils");
const logger = require("../utils/logger.utils");
import type { NextFunction, Request, Response } from "express";
import { logger } from "../utils/logger.utils";
import { isAuthed } from "../utils/auth.utils";
module.exports = async function authCheck(
req: Request,
res: Response,
next: NextFunction
) {
logger.logger.debug("auth.middleware: Auth check begun");
logger.debug("auth.middleware: Auth check begun");
if (process.env.NODE_ENV === "development") {
req.isAuthed = true;
logger.logger.warn("auth.middleware: DEV MODE - Access Granted");
logger.warn("auth.middleware: DEV MODE - Access Granted");
next();
} else {
const id: string | string[] | undefined = req.headers.uuid;
if (typeof id === "undefined") {
req.isAuthed = false;
logger.logger.info("auth.middleware: Authentication failed");
logger.info("auth.middleware: Authentication failed");
next();
} else if (typeof id === "string") {
const authCheck = (await utils.isAuthed(id)) || false;
const authCheck = (await isAuthed(id)) || false;
if (authCheck) {
req.isAuthed = true;
logger.logger.info("auth.middleware: Authentication Successful");
logger.info("auth.middleware: Authentication Successful");
next();
} else {
req.isAuthed = false;
logger.logger.info("auth.middleware: Authentication Failed");
logger.info("auth.middleware: Authentication Failed");
next();
}
} else if (Array.isArray(id)) {
const authCheck = (await utils.isAuthed(id[0])) || false;
const authCheck = (await isAuthed(id[0])) || false;
if (authCheck) {
req.isAuthed = true;
logger.logger.warn(
logger.warn(
"auth.middleware: UUID Passed as Array - Authentication Successful"
);
next();
} else {
req.isAuthed = false;
logger.logger.warn(
logger.warn(
"auth.middleware: UUID Passed as Array - Authentication Failed"
);
next();