🎨 (jwt helper) create type and add details in error scope

create a type for JWT encode and add error details in JWT decode error scope
This commit is contained in:
rafiarrafif
2025-06-24 16:45:39 +07:00
parent 5469fd7ab3
commit ba6935c31b
2 changed files with 2 additions and 2 deletions

View File

@ -11,6 +11,6 @@ export const jwtDecode = (payload: string) => {
const decodedPayload = jwt.verify(payload, JWTKey); const decodedPayload = jwt.verify(payload, JWTKey);
return decodedPayload as JWTSessionPayload; return decodedPayload as JWTSessionPayload;
} catch (error) { } catch (error) {
throw new AppError(401, "Invalid or expired token"); throw new AppError(401, "Invalid or expired token", error);
} }
}; };

View File

@ -1,6 +1,6 @@
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
export const jwtEncode = (payload: any) => { export const jwtEncode = <T extends object>(payload: T) => {
const tokenLifetime = Number(process.env.SESSION_EXPIRE!); const tokenLifetime = Number(process.env.SESSION_EXPIRE!);
const jwtSecret = process.env.JWT_SECRET!; const jwtSecret = process.env.JWT_SECRET!;
const jwtToken = jwt.sign(payload, jwtSecret, { const jwtToken = jwt.sign(payload, jwtSecret, {