signJWT (crypto/jwt.ts)

better-auth · packages/better-auth/src/crypto/jwt.ts · typescript · MIT
auth

Module exposing: signJWT, verifyJWT, symmetricEncodeJWT, symmetricDecodeJWT, deriveEncryptionSecret, getCurrentSecret, getAllSecrets

// Source: https://github.com/better-auth/better-auth | License: MIT | Copyright 2024 - present, Bereket Engida
// Provided via justclaw.it.com; keep this notice with the code.
import { hkdf } from "@noble/hashes/hkdf.js";
import { sha256 } from "@noble/hashes/sha2.js";
import {
	base64url,
	calculateJwkThumbprint,
	decodeProtectedHeader,
	EncryptJWT,
	jwtDecrypt,
	jwtVerify,
	SignJWT,
} from "jose";
import type { SecretConfig } from "./index";

export async function signJWT(
	payload: any,
	secret: string,
	expiresIn: number = 3600,
): Promise<string> {
	const jwt = await new SignJWT(payload)
		.setProtectedHeader({ alg: "HS256" })
		.setIssuedAt()
		.setExpirationTime(Math.floor(Date.now() / 1000) + expiresIn)
		.sign(new TextEncoder().encode(secret));

	return jwt;
}

export async funct

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key