From 08125dcb118bafe12cb4dc3d8fb7e76df6dddd3d Mon Sep 17 00:00:00 2001 From: Vivy Bot Date: Tue, 10 Feb 2026 23:12:04 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20logout=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/helpers/http/jwt/decode/index.ts | 5 ++--- .../auth/controllers/logout.controller.ts | 19 +++++++++++++++++++ src/modules/auth/index.ts | 4 +++- .../auth/services/http/logout.service.ts | 14 ++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/modules/auth/controllers/logout.controller.ts create mode 100644 src/modules/auth/services/http/logout.service.ts diff --git a/src/helpers/http/jwt/decode/index.ts b/src/helpers/http/jwt/decode/index.ts index aaf5fe5..caed720 100644 --- a/src/helpers/http/jwt/decode/index.ts +++ b/src/helpers/http/jwt/decode/index.ts @@ -1,15 +1,14 @@ import jwt from "jsonwebtoken"; -import { JWTSessionPayload } from "../../../../modules/auth/auth.types"; +import { JWTAuthToken } from "./types"; import { AppError } from "../../../error/instances/app"; export const jwtDecode = (payload: string) => { - // return payload; if (!payload) throw new AppError(401, "Unauthorized"); const JWTKey = process.env.JWT_SECRET!; try { const decodedPayload = jwt.verify(payload, JWTKey); - return decodedPayload as JWTSessionPayload; + return decodedPayload as JWTAuthToken; } catch (error) { throw new AppError(401, "Invalid or expired token", error); } diff --git a/src/modules/auth/controllers/logout.controller.ts b/src/modules/auth/controllers/logout.controller.ts new file mode 100644 index 0000000..4d8f23f --- /dev/null +++ b/src/modules/auth/controllers/logout.controller.ts @@ -0,0 +1,19 @@ +import { Context } from "elysia"; +import { mainErrorHandler } from "../../../helpers/error/handler"; +import { logoutService } from "../services/http/logout.service"; +import { returnWriteResponse } from "../../../helpers/callback/httpResponse"; + +export const logoutController = async (ctx: Context) => { + try { + const jwtToken = ctx.cookie.auth_token?.value; + const serviceResponse = await logoutService(jwtToken); + return returnWriteResponse( + ctx.set, + 200, + "Logout successful", + serviceResponse, + ); + } catch (error) { + return mainErrorHandler(ctx.set, error); + } +}; diff --git a/src/modules/auth/index.ts b/src/modules/auth/index.ts index b6abb6d..bf0db41 100644 --- a/src/modules/auth/index.ts +++ b/src/modules/auth/index.ts @@ -6,6 +6,7 @@ import { googleCallbackController } from "./controllers/googleCallback.controlle import { getOauthProvidersController } from "./controllers/getOauthProviders.controller"; import { getCallbackProviderUrlController } from "./controllers/getCallbackProviderUrl.controller"; import { tokenValidationController } from "./controllers/tokenValidation.controller"; +import { logoutController } from "./controllers/logout.controller"; export const authModule = new Elysia({ prefix: "/auth" }) .post("/token/validate", tokenValidationController) @@ -14,4 +15,5 @@ export const authModule = new Elysia({ prefix: "/auth" }) .get("/github", githubRequestController) .get("/github/callback", githubCallbackController) .get("/google", googleRequestController) - .get("/google/callback", googleCallbackController); + .get("/google/callback", googleCallbackController) + .post("/logout", logoutController); diff --git a/src/modules/auth/services/http/logout.service.ts b/src/modules/auth/services/http/logout.service.ts new file mode 100644 index 0000000..0662960 --- /dev/null +++ b/src/modules/auth/services/http/logout.service.ts @@ -0,0 +1,14 @@ +import { AppError } from "../../../../helpers/error/instances/app"; +import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder"; +import { jwtDecode } from "../../../../helpers/http/jwt/decode"; + +export const logoutService = async (jwtToken?: any) => { + try { + if (!jwtToken) throw new AppError(403, "No auth token provided"); + + const jwtPayload = jwtDecode(jwtToken); + return jwtPayload; + } catch (error) { + ErrorForwarder(error); + } +};