From c72a06adb4ba6e038e1a3fcb2a15c9ee75771c64 Mon Sep 17 00:00:00 2001 From: rafiarrafif Date: Mon, 12 May 2025 21:58:13 +0700 Subject: [PATCH] rewrite jwt set in login service --- .../{cookies => http}/jwt/decode/index.ts | 17 ----------------- .../{cookies => http}/jwt/decode/types.ts | 0 .../{cookies => http}/jwt/encode/index.ts | 0 .../getUserHeaderInformation/index.ts | 0 .../getUserHeaderInformation/types.ts | 0 .../controller/loginWithPassword.controller.ts | 2 +- .../auth/services/loginWithPassword.service.ts | 9 +++++---- .../controller/createUserRole.controller.ts | 2 +- .../controllers/createUserSession.controller.ts | 2 +- src/modules/userSession/userSession.types.ts | 2 +- 10 files changed, 9 insertions(+), 25 deletions(-) rename src/helpers/{cookies => http}/jwt/decode/index.ts (53%) rename src/helpers/{cookies => http}/jwt/decode/types.ts (100%) rename src/helpers/{cookies => http}/jwt/encode/index.ts (100%) rename src/helpers/{cookies => http}/userHeader/getUserHeaderInformation/index.ts (100%) rename src/helpers/{cookies => http}/userHeader/getUserHeaderInformation/types.ts (100%) diff --git a/src/helpers/cookies/jwt/decode/index.ts b/src/helpers/http/jwt/decode/index.ts similarity index 53% rename from src/helpers/cookies/jwt/decode/index.ts rename to src/helpers/http/jwt/decode/index.ts index 2f1f92b..997bca2 100644 --- a/src/helpers/cookies/jwt/decode/index.ts +++ b/src/helpers/http/jwt/decode/index.ts @@ -4,23 +4,6 @@ import { JWTAuthToken } from "./types"; import { parse } from "cookie"; import { returnErrorResponse } from "../../../callback/httpResponse"; -/** - * Verifies the authentication cookie from the request header. - * - * This helper function is used in an ElysiaJS context to check the validity of - * a user's authentication token stored in cookies. If the cookie is not found, - * it returns a `400 Bad Request`. If the token is invalid or expired, it returns - * a `401 Unauthorized`. If the token is valid, it returns the decoded user data. - * - * @param ctx - The request context from Elysia, used to read headers and set the response. - * - * @returns The decoded JWT payload if the token is valid, - * or a standardized error response if the cookie is missing or invalid. - * - * @example - * const decodedToken = decodeAuthToken(ctx); - * ctx => Elysia context - */ export const JWTDecodeToken = (ctx: Context): JWTAuthToken => { const cookiePayload = ctx.request.headers.get("Cookie"); if (!cookiePayload) diff --git a/src/helpers/cookies/jwt/decode/types.ts b/src/helpers/http/jwt/decode/types.ts similarity index 100% rename from src/helpers/cookies/jwt/decode/types.ts rename to src/helpers/http/jwt/decode/types.ts diff --git a/src/helpers/cookies/jwt/encode/index.ts b/src/helpers/http/jwt/encode/index.ts similarity index 100% rename from src/helpers/cookies/jwt/encode/index.ts rename to src/helpers/http/jwt/encode/index.ts diff --git a/src/helpers/cookies/userHeader/getUserHeaderInformation/index.ts b/src/helpers/http/userHeader/getUserHeaderInformation/index.ts similarity index 100% rename from src/helpers/cookies/userHeader/getUserHeaderInformation/index.ts rename to src/helpers/http/userHeader/getUserHeaderInformation/index.ts diff --git a/src/helpers/cookies/userHeader/getUserHeaderInformation/types.ts b/src/helpers/http/userHeader/getUserHeaderInformation/types.ts similarity index 100% rename from src/helpers/cookies/userHeader/getUserHeaderInformation/types.ts rename to src/helpers/http/userHeader/getUserHeaderInformation/types.ts diff --git a/src/modules/auth/controller/loginWithPassword.controller.ts b/src/modules/auth/controller/loginWithPassword.controller.ts index 6d0af31..a51ad11 100644 --- a/src/modules/auth/controller/loginWithPassword.controller.ts +++ b/src/modules/auth/controller/loginWithPassword.controller.ts @@ -7,7 +7,7 @@ import { } from "../../../helpers/callback/httpResponse"; import { LoginWithPasswordRequest } from "../auth.types"; import { mainErrorHandler } from "../../../helpers/error/handler"; -import { getUserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation"; +import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation"; export const loginWithPassword = async ( ctx: Context & { body: LoginWithPasswordRequest } diff --git a/src/modules/auth/services/loginWithPassword.service.ts b/src/modules/auth/services/loginWithPassword.service.ts index fff4ed8..5022250 100644 --- a/src/modules/auth/services/loginWithPassword.service.ts +++ b/src/modules/auth/services/loginWithPassword.service.ts @@ -2,10 +2,9 @@ import bcrypt from "bcrypt"; import { findUserByEmailOrUsernameService } from "../../user/services/findUserByEmailOrUsername.service"; import { LoginWithPasswordRequest } from "../auth.types"; import { AppError } from "../../../helpers/error/instances/app"; -import { UserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation/types"; +import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types"; import { createUserSessionService } from "../../userSession/services/createUserSession.service"; -import { jwtEncode } from "../../../helpers/cookies/jwt/encode"; -import { returnReadResponse } from "../../../helpers/callback/httpResponse"; +import { jwtEncode } from "../../../helpers/http/jwt/encode"; export const loginWithPasswordService = async ( request: LoginWithPasswordRequest, @@ -15,15 +14,17 @@ export const loginWithPasswordService = async ( // search for user data using an identifier (username or email) const userData = await findUserByEmailOrUsernameService(request.identifier); - // Validate the password in the request with the existing one + // validate the password in the request with the existing one if (!(await bcrypt.compare(request.password, userData.password))) throw new AppError(401, "Password incorrect"); + // create new user session const userSession = await createUserSessionService({ userId: userData.id, userHeaderInformation: userHeaderInfo, }); + // create JWT token that contain user session const jwtToken = jwtEncode(userSession); return jwtToken; diff --git a/src/modules/userRole/controller/createUserRole.controller.ts b/src/modules/userRole/controller/createUserRole.controller.ts index ed8eabd..654b5fc 100644 --- a/src/modules/userRole/controller/createUserRole.controller.ts +++ b/src/modules/userRole/controller/createUserRole.controller.ts @@ -5,7 +5,7 @@ import { returnWriteResponse, } from "../../../helpers/callback/httpResponse"; import { createUserRoleService } from "../services/createUserRole.service"; -import { JWTDecodeToken } from "../../../helpers/cookies/jwt/decode"; +import { JWTDecodeToken } from "../../../helpers/http/jwt/decode"; import { mainErrorHandler } from "../../../helpers/error/handler"; import { createUserRoleSchema } from "../schemas/createUserRole.schema"; diff --git a/src/modules/userSession/controllers/createUserSession.controller.ts b/src/modules/userSession/controllers/createUserSession.controller.ts index 060df7b..9d15ee2 100644 --- a/src/modules/userSession/controllers/createUserSession.controller.ts +++ b/src/modules/userSession/controllers/createUserSession.controller.ts @@ -1,6 +1,6 @@ import { Context } from "elysia"; import { createUserSessionService } from "../services/createUserSession.service"; -import { getUserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation"; +import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation"; import { mainErrorHandler } from "../../../helpers/error/handler"; import { returnErrorResponse, diff --git a/src/modules/userSession/userSession.types.ts b/src/modules/userSession/userSession.types.ts index c4bd913..f451af3 100644 --- a/src/modules/userSession/userSession.types.ts +++ b/src/modules/userSession/userSession.types.ts @@ -1,4 +1,4 @@ -import { UserHeaderInformation } from "../../helpers/cookies/userHeader/getUserHeaderInformation/types"; +import { UserHeaderInformation } from "../../helpers/http/userHeader/getUserHeaderInformation/types"; export interface createUserSessionServiceParams { userId: string;