Skip to content
better-auth-jwt-jwks logo

Better Auth JWT and JWKS Implementation Skill

better-auth-jwt-jwks

Expert skill for implementing Better Auth with JWT tokens and JWKS (JSON Web Key Set) for secure authentication between Next.js frontend and FastAPI backend. Handles JWT token generation, verification, JWKS endpoint setup, and secure API communication. Includes setup for database integration, ses...

SKILL.md

Full skill instructions

Better Auth JWT and JWKS Implementation Skill

When to Use This Skill

  • User wants to implement Better Auth with JWT tokens for Next.js + FastAPI authentication
  • Need to set up secure communication between frontend and backend services
  • Want to implement user isolation with JWT token verification
  • Need JWKS endpoint for public key distribution and token verification
  • Looking for stateless authentication without server-side sessions

How This Skill Works (Step-by-Step Execution)

  1. Better Auth Configuration

    • Set up Better Auth with JWT plugin for token generation
    • Configure database connection (PostgreSQL recommended)
    • Enable email/​password and social authentication
    • Configure JWT algorithm (RS256 or EdDSA) and expiration
  2. JWKS Endpoint Setup (Frontend)

    • Create /​api/​jwks endpoint to expose public keys
    • Implement database storage for JWKS keys
    • Add caching headers for performance optimization
    • Configure key rotation and management
  3. FastAPI JWT Verification

    • Install pyjwt and cryptography dependencies
    • Create JWKS client to fetch public keys
    • Implement JWT verification middleware
    • Create dependency injection for user authentication
  4. Secure API Implementation

    • Protect API routes with JWT verification
    • Implement user isolation (verify user_id matches request)
    • Add proper error handling for token issues
    • Configure environment variables for shared secrets

Output You Will Receive

After activation, I will deliver:

  • Complete Better Auth configuration with JWT plugin
  • JWKS endpoint implementation for key distribution
  • FastAPI middleware for JWT token verification
  • Protected API route examples with user isolation
  • Environment variable setup for both services
  • Database schema for JWKS storage
  • Error handling and security best practices

Example Usage

User says: "I have a Next.js 14 App with Better Auth and need to secure my FastAPI backend with JWT tokens."

This Skill Instantly Activates → Delivers:

  • Better Auth JWT plugin configuration
  • /​api/​jwks endpoint in Next.js for public key exposure
  • FastAPI middleware using pyjwt and JWKS client
  • Protected route examples with user isolation
  • Shared secret configuration between services
  • Complete setup for secure frontend-backend communication

User says: "Implement JWT authentication between my Next.js frontend and FastAPI backend."

This Skill Responds: → Sets up Better Auth with JWT generation on frontend → Creates JWKS endpoint for key distribution → Implements JWT verification in FastAPI backend → Provides user isolation and security best practices

Activate This Skill By Saying

  • "Set up Better Auth with JWT and JWKS"
  • "Secure my FastAPI backend with Better Auth JWT tokens"
  • "Implement JWT authentication between Next.js and FastAPI"
  • "I need user isolation with JWT tokens"

Core Implementation Steps

1. Better Auth Setup (Frontend)

import { betterAuth } from "better-auth";
import { jwt } from "better-auth/​plugins";

export const auth = betterAuth({
  database: {
    provider: "postgres",
    url: process.env.DATABASE_URL,
  },
  emailAndPassword: {
    enabled: true,
  },
  plugins: [
    jwt({
      algorithm: "RS256",
      expiresIn: "7d",
      issuer: process.env.ISSUER_URL,
    }),
  ],
});

2. JWKS Endpoint (Frontend)

export async function GET() {
  const { db } = await import("@/​lib/​db");
  const { jwks } = await import("@/​drizzle/​schema");

  const keys = await db.select().from(jwks);

  const jwksResponse = {
    keys: keys.map((key) => {
      const publicKey = JSON.parse(key.publicKey);
      return {
        ...publicKey,
        kid: key.id,
      };
    }),
  };

  return new Response(JSON.stringify(jwksResponse), {
    status: 200,
    headers: {
      "Content-Type": "application/​json",
      "Cache-Control": "public, max-age=3600",
    },
  });
}

3. FastAPI JWT Verification

from jwt import PyJWKClient
import jwt
from fastapi import Depends, HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials

security = HTTPBearer()

def get_jwk_client():
    jwks_url = f"{settings.better_auth_url}/​.well-known/​jwks.json"
    return PyJWKClient(jwks_url)

def verify_jwt_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
    token = credentials.credentials
    try:
        jwk_client = get_jwk_client()
        signing_key = jwk_client.get_signing_key_from_jwt(token)

        payload = jwt.decode(
            token,
            signing_key.key,
            algorithms=["RS256"],
            options={"verify_aud": False}
        )

        user_id = payload.get("sub") or payload.get("user_id")
        if not user_id:
            raise ValueError("Invalid token: missing user_id")

        return {"user_id": user_id, "email": payload.get("email", "")}
    except jwt.exceptions.PyJWTError as e:
        raise HTTPException(status_code=401, detail=f"Invalid token: {str(e)}")

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View