jwt
JWT (JSON Web Token) patterns for authentication. Use when implementing token-based authentication, verifying tokens, or configuring JWT in Better Auth.
When & Why to Use This Skill
This Claude skill provides comprehensive patterns and implementation guides for JWT (JSON Web Token) authentication. It streamlines the process of setting up stateless security between frontends and backends, offering ready-to-use configurations for Better Auth and verification logic for FastAPI. By utilizing this skill, developers can ensure secure token handling, claim verification, and robust identity management in modern web applications.
Use Cases
- Implementing stateless user authentication for web applications to improve scalability.
- Configuring Better Auth on the frontend to manage JWT issuance, storage, and expiration.
- Developing backend verification middleware in FastAPI to decode tokens and validate user permissions.
- Defining and extracting custom JWT claims (like user ID and email) to manage session context across services.
- Establishing secure communication patterns between decoupled frontend and backend architectures.
| name | jwt |
|---|---|
| description | JWT (JSON Web Token) patterns for authentication. Use when implementing token-based authentication, verifying tokens, or configuring JWT in Better Auth. |
| allowed-tools | Read, Edit, Write, Glob, Grep, Bash |
JWT Skill
Quick Reference
JWT is used for stateless authentication between frontend (Better Auth) and backend (FastAPI).
JWT Structure
Header.Payload.Signature
# Header
{
"alg": "HS256",
"typ": "JWT"
}
# Payload (Claims)
{
"sub": "user_123",
"email": "user@example.com",
"name": "John Doe",
"iat": 1700000000,
"exp": 1700600000
}
# Signature
HMAC-SHA256(header.payload, SECRET)
Better Auth JWT Configuration
// frontend/lib/auth.ts
import { createAuth } from "better-auth"
import { jwt } from "better-auth/plugins"
export const auth = createAuth({
plugins: [
jwt({
jwt: {
secret: process.env.BETTER_AUTH_SECRET!,
expiry: "7d",
},
}),
],
})
Backend JWT Verification (Python)
# backend/auth.py
import jwt
from jwt.exceptions import PyJWTError
BETTER_AUTH_SECRET = os.getenv("BETTER_AUTH_SECRET")
async def verify_jwt(token: str) -> dict:
try:
payload = jwt.decode(
token,
BETTER_AUTH_SECRET,
algorithms=["HS256"]
)
return payload
except PyJWTError as e:
raise HTTPException(status_code=401, detail=f"Invalid token: {e}")
async def get_current_user(credentials) -> str:
payload = await verify_jwt(credentials.credentials)
return payload.get("sub")
Token Claims
| Claim | Meaning |
|---|---|
sub |
Subject (user ID) |
email |
User email |
name |
User name |
iat |
Issued at (timestamp) |
exp |
Expiration (timestamp) |
iss |
Issuer |
aud |
Audience |
For Detailed Reference
See REFERENCE.md for:
- Token refresh
- Token revocation
- Security best practices
- Debugging JWT issues