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");
}
};