first
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { sign as jwtSign, verify as jwtVerify } from 'hono/jwt';
|
||||
const { JWT_SECRET } = process.env;
|
||||
|
||||
function parseExpiration(expiration: string): number {
|
||||
const match = expiration.match(/^(\d+)([smhd])$/);
|
||||
if (!match) {
|
||||
throw new Error(`Invalid expiration format: ${expiration}`);
|
||||
}
|
||||
const value = parseInt(match[1]!, 10);
|
||||
const unit = match[2];
|
||||
const multipliers: Record<string, number> = {
|
||||
s: 1,
|
||||
m: 60,
|
||||
h: 60 * 60,
|
||||
d: 60 * 60 * 24,
|
||||
};
|
||||
return Math.floor(Date.now() / 1000) + value * multipliers[unit!]!;
|
||||
}
|
||||
|
||||
export async function sign(payload: any, expiration = '30d') {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const exp = parseExpiration(expiration);
|
||||
const jti = crypto.randomUUID();
|
||||
const data = { ...payload, exp, iat: now, jti };
|
||||
return jwtSign(data, JWT_SECRET!);
|
||||
}
|
||||
|
||||
export async function verify(token: string): Promise<any> {
|
||||
return jwtVerify(token, JWT_SECRET!);
|
||||
}
|
||||
Reference in New Issue
Block a user