Created
February 4, 2023 12:11
-
-
Save LunaticMuch/63696e2a001a292bd2c839c92941c10c to your computer and use it in GitHub Desktop.
Token generation with jose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {SignJWT, jwtVerify, type JWTPayload} from 'jose'; | |
export async function sign(payload: Token, secret: string): Promise<string> { | |
const iat = Math.floor(Date.now() / 1000); | |
const exp = iat + 60* 60; // one hour | |
return new SignJWT({...payload}) | |
.setProtectedHeader({alg: 'HS256', typ: 'JWT'}) | |
.setExpirationTime(exp) | |
.setIssuedAt(iat) | |
.setNotBefore(iat) | |
.sign(new TextEncoder().encode(secret)); | |
} | |
export async function verify(token: string, secret: string): Promise<Token> { | |
const {payload} = await jwtVerify(token, new TextEncoder().encode(secret)); | |
// run some checks on the returned payload, perhaps you expect some specific values | |
// if its all good, return it, or perhaps just return a boolean | |
return payload; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment