diff --git a/utils/Jwt.ts b/utils/Jwt.ts new file mode 100644 index 0000000..d571e43 --- /dev/null +++ b/utils/Jwt.ts @@ -0,0 +1,26 @@ +import * as fs from 'fs' +import * as jwt from 'jsonwebtoken' + +const publicPem = fs.readFileSync('./public.pem') +const privateKey = fs.readFileSync('./private.key') + +export function sign(payload: string | Record, options?: JWTSignOption): string | null { + try { + return jwt.sign(payload, privateKey, options ? { ...options, algorithm: 'RS256' } : { algorithm: 'RS256' }) + } catch { + return null + } +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function verify(token: string): any | null { + try { + return jwt.verify(token, publicPem) + } catch { + return null + } +} + +interface JWTSignOption { + expiresIn: number | string +}