finishing auth controller verification

This commit is contained in:
rafiarrafif
2025-05-13 14:35:48 +07:00
parent ad9f66a642
commit 9cb84372b8
7 changed files with 78 additions and 28 deletions

View File

@ -1,29 +1,14 @@
import jwt from "jsonwebtoken";
import { Context } from "elysia";
import { JWTAuthToken } from "./types";
import { parse } from "cookie";
import { returnErrorResponse } from "../../../callback/httpResponse";
export const JWTDecodeToken = (ctx: Context): JWTAuthToken => {
const cookiePayload = ctx.request.headers.get("Cookie");
if (!cookiePayload)
throw returnErrorResponse(ctx.set, 400, "Bad Request", "No cookies found");
const cookies = parse(cookiePayload);
const cookiesToken = cookies.auth_token!;
export const jwtDecode = (payload: string) => {
// return payload;
if (!payload) throw "JWT decode payload not found";
const JWTKey = process.env.JWT_SECRET!;
try {
const decodedToken = jwt.verify(
cookiesToken,
process.env.JWT_SECRET!
) as JWTAuthToken;
return decodedToken;
const decodedPayload = jwt.verify(payload, JWTKey);
return decodedPayload;
} catch (error) {
throw returnErrorResponse(
ctx.set,
401,
"Unauthorized",
"Invalid or expired token"
);
throw "JWT expired or not valid";
}
};

View File

@ -0,0 +1,13 @@
import { parse } from "cookie";
import { Context } from "elysia";
import { AppError } from "../../../error/instances/app";
export const getCookie = (ctx: Context) => {
try {
const cookiePayload = ctx.request.headers.get("Cookie");
const cookies = parse(cookiePayload!);
return cookies;
} catch (error) {
throw new AppError(401, "Cookie not found");
}
};

View File

@ -0,0 +1,10 @@
import { Context } from "elysia";
export const authMiddleware = (ctx: Context) => {
const token = ctx.cookie.auth_token;
if (!token) {
ctx.set.status = 401;
throw "Unauthorized: Token missing";
}
};

View File

@ -0,0 +1,21 @@
import { Context } from "elysia";
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
import { authVerificationService } from "../services/authVerification.service";
import { mainErrorHandler } from "../../../helpers/error/handler";
import {
returnErrorResponse,
returnWriteResponse,
} from "../../../helpers/callback/httpResponse";
export const authVerification = async (ctx: Context) => {
try {
const cookie = getCookie(ctx);
if (!cookie.auth_token)
return returnErrorResponse(ctx.set, 401, "Auth token not found");
const authService = authVerificationService(cookie.auth_token);
return returnWriteResponse(ctx.set, 200, "User authenticated", authService);
} catch (error) {
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -1,7 +1,17 @@
import Elysia from "elysia";
import { loginWithPassword } from "./controller/loginWithPassword.controller";
import { authMiddleware } from "../../middleware/auth.middleware";
import { authVerification } from "./controller/authVerification.controller";
export const authModule = new Elysia({ prefix: "/auth" }).post(
"/legacy",
loginWithPassword
);
export const authModule = new Elysia({ prefix: "/auth" })
.post("/legacy", loginWithPassword)
.post("/verification", authVerification)
.get(
"/test",
() => {
return "PASSED";
},
{
beforeHandle: authMiddleware,
}
);

View File

@ -0,0 +1,12 @@
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 {
const userToken = jwtDecode(cookie);
return userToken;
} catch (error) {
throw new AppError(401, "Token is invalid");
}
};

View File

@ -5,7 +5,6 @@ import {
returnWriteResponse,
} from "../../../helpers/callback/httpResponse";
import { createUserRoleService } from "../services/createUserRole.service";
import { JWTDecodeToken } from "../../../helpers/http/jwt/decode";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
@ -51,7 +50,7 @@ export const createUserRole = async (
const formData: Prisma.UserRoleUncheckedCreateInput = {
...ctx.body,
createdBy: JWTDecodeToken(ctx).user.id,
createdBy: "daw",
};
try {