Skip to content

Instantly share code, notes, and snippets.

@ShivamJoker
Last active September 6, 2022 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShivamJoker/aee6ef8b9e485d5982a440ffc212e4d1 to your computer and use it in GitHub Desktop.
Save ShivamJoker/aee6ef8b9e485d5982a440ffc212e4d1 to your computer and use it in GitHub Desktop.
Simple JWT encoding and verifying with jose library
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: "cool@guy.com"
})
.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