Email Unsubscribe Token Utility (lib/unsubscribe-token.ts)

velobase-harness · src/lib/unsubscribe-token.ts · typescript · MIT
auth, security, email

Generates and verifies secure JWT-based tokens for handling one-click user email unsubscriptions.

// Source: https://github.com/velobase/velobase-harness | License: MIT | Copyright 2026 Velobase
// Provided via justclaw.it.com; keep this notice with the code.
import { SignJWT, jwtVerify } from "jose";
import { env } from "@/env";

const SECRET = new TextEncoder().encode(
  env.RESEND_WEBHOOK_SECRET || env.AUTH_SECRET || "fallback_secret",
);

export interface UnsubscribePayload {
  uid: string; // User ID
  act: "email_unsubscribe";
}

export async function generateUnsubscribeToken(userId: string): Promise<string> {
  return await new SignJWT({ uid: userId, act: "email_unsubscribe" })
    .setProtectedHeader({ alg: "HS256" })
    .setIssuedAt()
    .setExpirationTime("30d") // 30天有效期
    .sign(SECRET);
}

export async function verifyUnsubscribeToken(token: string): Promise<string | nul

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

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

Get a free API key