From 87532b001d4ddbc8ee5d736eaa8f705b3720b8b5 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sun, 10 Mar 2024 21:01:00 +0000 Subject: [PATCH] Tidy auth middleware Signed-off-by: Fred Boniface --- src/middlewares/auth.middlewares.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/middlewares/auth.middlewares.ts b/src/middlewares/auth.middlewares.ts index ea6f105..3eea2eb 100644 --- a/src/middlewares/auth.middlewares.ts +++ b/src/middlewares/auth.middlewares.ts @@ -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();