Last active
September 6, 2022 17:48
-
-
Save ShivamJoker/aee6ef8b9e485d5982a440ffc212e4d1 to your computer and use it in GitHub Desktop.
Simple JWT encoding and verifying with jose library
This file contains 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 { TextEncoder } from "util"; | |
import { SignJWT, jwtVerify } from "jose"; | |
const secret = process.env.JWT_SECRET ?? "I like bananas"; | |
const textEncoder = new TextEncoder(); | |
const keyToSignWith = textEncoder.encode(secret); | |
const jwt = await new SignJWT({ | |
email: "[email protected]" | |
}) | |
.setExpirationTime('12h') | |
.setProtectedHeader({ alg: "HS256" }) | |
.sign(keyToSignWith); | |
console.log("jwt", jwt); | |
const { payload } = await jwtVerify(jwt, keyToSignWith); | |
console.log(payload); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment