core/utils/Jwt.ts
SKINMAKER b421d1ab64
chore: apply prettier (#637)
* chore: apply prettier

* chore: edit ready comment

* chore: move ts comment
2023-11-29 22:04:33 +09:00

37 lines
837 B
TypeScript

import * as jwt from 'jsonwebtoken'
const publicPem = process.env.PUBLIC_PEM?.replace(/\\n/g, '\n')
const privateKey = process.env.PRIVATE_KEY?.replace(/\\n/g, '\n')
export function sign(
payload: string | Record<string, unknown>,
options?: JWTSignOption
): string | null {
try {
return jwt.sign(
payload,
privateKey,
options
? { ...options, algorithm: 'RS256', allowInsecureKeySizes: true }
: { algorithm: 'RS256', allowInsecureKeySizes: true }
)
} catch {
return null
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function verify(token?: string): any | null {
if (!token) return null
try {
return jwt.verify(token, publicPem)
} catch (e) {
if (e.name !== 'TokenExpiredError') console.error(e)
return null
}
}
interface JWTSignOption {
expiresIn: number | string
}