mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 06:58:46 +00:00
/api/auth/logout, /api/auth/refreshToken Endpoint 구현
This commit is contained in:
38
src/lib/server/modules/auth.ts
Normal file
38
src/lib/server/modules/auth.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import jwt from "jsonwebtoken";
|
||||
import env from "$lib/server/loadenv";
|
||||
|
||||
interface TokenData {
|
||||
type: "access" | "refresh";
|
||||
userId: number;
|
||||
clientId?: number;
|
||||
}
|
||||
|
||||
export enum TokenError {
|
||||
EXPIRED,
|
||||
INVALID,
|
||||
}
|
||||
|
||||
export const issueToken = (type: "access" | "refresh", userId: number, clientId?: number) => {
|
||||
return jwt.sign(
|
||||
{
|
||||
type,
|
||||
userId,
|
||||
clientId,
|
||||
} satisfies TokenData,
|
||||
env.jwt.secret,
|
||||
{
|
||||
expiresIn: type === "access" ? env.jwt.accessExp : env.jwt.refreshExp,
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
export const verifyToken = (token: string) => {
|
||||
try {
|
||||
return jwt.verify(token, env.jwt.secret) as TokenData;
|
||||
} catch (error) {
|
||||
if (error instanceof jwt.TokenExpiredError) {
|
||||
return TokenError.EXPIRED;
|
||||
}
|
||||
return TokenError.INVALID;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user