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

@ -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 {