chatbot-template/lib/cookies/encrypt-jwe.ts
2023-05-19 14:13:38 -04:00

16 lines
477 B
TypeScript

import { EncryptJWT, base64url } from 'jose';
export async function encryptJWECookie<T extends string | object = any>(
payload: T,
expirationTime: string,
secret: string | undefined = process.env.JWE_SECRET
): Promise<string> {
if (!secret) {
throw new Error('Missing JWE secret');
}
return new EncryptJWT(payload as any)
.setExpirationTime(expirationTime)
.setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
.encrypt(base64url.decode(secret));
}