diff --git a/src/middleware/auth.middleware.ts b/src/middleware/auth.middleware.ts index 54280ee..b7bed40 100644 --- a/src/middleware/auth.middleware.ts +++ b/src/middleware/auth.middleware.ts @@ -1,10 +1,14 @@ import { Context } from "elysia"; +import { getCookie } from "../helpers/http/userHeader/cookies/getCookies"; +import { mainErrorHandler } from "../helpers/error/handler"; +import { returnErrorResponse } from "../helpers/callback/httpResponse"; export const authMiddleware = (ctx: Context) => { - const token = ctx.cookie.auth_token; - - if (!token) { - ctx.set.status = 401; - throw "Unauthorized: Token missing"; + try { + const cookie = getCookie(ctx); + if (!cookie.auth_token) + return returnErrorResponse(ctx.set, 401, "User Unauthorized"); + } catch (error) { + return mainErrorHandler(ctx.set, error); } }; diff --git a/src/modules/auth/index.ts b/src/modules/auth/index.ts index f43fba0..9dba3bc 100644 --- a/src/modules/auth/index.ts +++ b/src/modules/auth/index.ts @@ -5,13 +5,6 @@ import { authVerification } from "./controller/authVerification.controller"; export const authModule = new Elysia({ prefix: "/auth" }) .post("/legacy", loginWithPassword) - .post("/verification", authVerification) - .get( - "/test", - () => { - return "PASSED"; - }, - { - beforeHandle: authMiddleware, - } - ); + .post("/verification", authVerification, { + beforeHandle: authMiddleware, + }); diff --git a/src/modules/auth/services/authVerification.service.ts b/src/modules/auth/services/authVerification.service.ts index a2205e1..f6989ad 100644 --- a/src/modules/auth/services/authVerification.service.ts +++ b/src/modules/auth/services/authVerification.service.ts @@ -1,6 +1,5 @@ import { AppError } from "../../../helpers/error/instances/app"; import { jwtDecode } from "../../../helpers/http/jwt/decode"; -import { jwtEncode } from "../../../helpers/http/jwt/encode"; export const authVerificationService = (cookie: string) => { try {