mirror of
https://github.com/koreanbots/core.git
synced 2025-12-16 14:30:22 +00:00
feat: added Jwt
This commit is contained in:
parent
b6c5467cdf
commit
19d79086c1
26
utils/Jwt.ts
Normal file
26
utils/Jwt.ts
Normal file
@ -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<string, unknown>, 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
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user