Loading...
Loading...
Implement secure authentication bridge between Better Auth (Next.js frontend) and FastAPI (Python backend) using JWKS JWT token verification. Use this skill when users need to (1) Integrate Better Auth with FastAPI backend, (2) Implement JWT authentication with JWKS verification, (3) Set up user isolation and authorization in FastAPI endpoints, (4) Configure frontend to send authenticated API requests, or (5) Troubleshoot Better Auth + FastAPI authentication issues.
npx skill4agent add bilalmk/todo_correct betterauth-fastapi-jwt-bridgeUser Login (Frontend)
↓
Better Auth → Issues JWT Token
↓
Frontend API Request → Authorization: Bearer <token>
↓
FastAPI Backend → Verifies JWT with JWKS → Returns filtered data// lib/auth.ts
import { betterAuth } from "better-auth"
import { jwt } from "better-auth/plugins"
export const auth = betterAuth({
plugins: [jwt()], // Enables JWT + JWKS endpoint
// ... other config
})# Next.js (Better Auth CLI)
npx @better-auth/cli migratesessiontokencolumn "token" of relation "session" does not existjwksrelation "jwks" does not existjwkssessionpython scripts/verify_jwks.py http://localhost:3000/api/auth/jwksassets/assets/jwt_verification.pybackend/app/auth/jwt_verification.pyassets/auth_dependencies.pybackend/app/auth/dependencies.pypip install fastapi python-jose[cryptography] pyjwt cryptography httpxfrom app.auth.dependencies import verify_user_access
@router.get("/{user_id}/tasks")
async def get_tasks(
user_id: str,
user: dict = Depends(verify_user_access)
):
# user_id is verified to match authenticated user
return get_user_tasks(user_id)assets/api_client.tsfrontend/lib/api-client.tsimport { getTasks, createTask } from "@/lib/api-client"
const tasks = await getTasks(userId)useSession()authClient.getSession()useEffectimport { useState, useEffect } from "react"
import { authClient } from "@/lib/auth-client"
function MyComponent() {
const [user, setUser] = useState(null)
useEffect(() => {
async function loadSession() {
const session = await authClient.getSession()
if (session?.data?.user) {
setUser(session.data.user)
}
}
loadSession()
}, [])
return <div>Welcome {user?.name}</div>
}iduuidCREATE TABLE "user" (
id VARCHAR PRIMARY KEY, -- Better Auth String ID
uuid UUID UNIQUE NOT NULL, -- Application UUID ⭐
email VARCHAR UNIQUE NOT NULL,
"emailVerified" BOOLEAN DEFAULT FALSE,
name VARCHAR,
"createdAt" TIMESTAMP NOT NULL,
"updatedAt" TIMESTAMP NOT NULL
);
-- UUID auto-generated by database
ALTER TABLE "user" ALTER COLUMN uuid SET DEFAULT gen_random_uuid();
-- All foreign keys point to user.uuid
CREATE TABLE tasks (
id UUID PRIMARY KEY,
user_id UUID REFERENCES "user"(uuid) ON DELETE CASCADE, -- ⭐ FK to uuid
title VARCHAR NOT NULL,
...
);// lib/auth.ts
import { betterAuth } from "better-auth"
import { jwt } from "better-auth/plugins"
import { Pool } from "pg"
const pool = new Pool({ connectionString: process.env.DATABASE_URL })
export const auth = betterAuth({
database: pool,
// Hook to fetch database-generated UUID
hooks: {
user: {
created: async ({ user }) => {
const result = await pool.query(
'SELECT uuid FROM "user" WHERE id = $1',
[user.id]
)
const uuid = result.rows[0]?.uuid
return { ...user, uuid }
}
}
},
// Include UUID in JWT payload
plugins: [
jwt({
algorithm: "EdDSA",
async jwt(user, session) {
return {
uuid: user.uuid, // ⭐ Custom claim for backend
}
},
}),
],
})sub# backend/app/auth/dependencies.py
from uuid import UUID
async def get_current_user(token: str = Depends(oauth2_scheme)) -> User:
payload = verify_jwt_token(token)
# Extract UUID from custom claim (not 'sub')
user_uuid_str = payload.get("uuid") # ⭐
user_uuid = UUID(user_uuid_str)
# Query by UUID
user = await session.execute(
select(User).where(User.uuid == user_uuid)
)
return user.scalar_one_or_none()
async def verify_user_match(
user_id: UUID, # From URL path
current_user: User = Depends(get_current_user)
) -> User:
# Compare UUIDs (not String IDs)
if current_user.uuid != user_id:
raise HTTPException(403, "Not authorized")
return current_userUser.uuidsubuser_iduser_idif current_user["user_id"] != user_id:
raise HTTPException(status_code=403, detail="Not authorized"){
"sub": "user_abc123", // Better Auth String ID
"uuid": "a1b2c3d4-e5f6...", // Application UUID (custom claim) ⭐
"email": "user@example.com",
"name": "User Name",
"iat": 1234567890, // Issued at
"exp": 1234567890, // Expiration
"iss": "http://localhost:3000",
"aud": "http://localhost:3000"
}uuidsubuuidBETTER_AUTH_SECRET="min-32-chars-secret"
BETTER_AUTH_URL="http://localhost:3000"
NEXT_PUBLIC_API_URL="http://localhost:8000"BETTER_AUTH_URL="http://localhost:3000"
DATABASE_URL="postgresql://..."python scripts/verify_jwks.py http://localhost:3000/api/auth/jwkskidktycrvxpython scripts/test_jwt_verification.py \
--jwks-url http://localhost:3000/api/auth/jwks \
--token "eyJhbGci..."| Issue | Solution |
|---|---|
| "relation 'jwks' does not exist" | Create JWKS table migration - see Database Schema Issues |
| "column 'token' does not exist" | Add |
| "Token missing UUID (uuid claim)" | Configure Better Auth hook and JWT plugin - see UUID Integration Issues |
| "User not found after registration" | Dual auth system conflict - see UUID Integration Issues |
| "authClient.useSession is not a function" | Use |
| "No authentication token available" | Use |
| "Unable to find matching signing key" | Clear JWKS cache in jwt_verification.py |
| "Token has expired" | Frontend needs to refresh session |
| "Invalid token claims" | Check issuer/audience match BETTER_AUTH_URL |
| 403 Forbidden (UUID mismatch) | Ensure UUID comparison, not String vs UUID - see UUID Integration Issues |
| Issue | Root Cause | Solution |
|---|---|---|
| Tasks not displaying despite 200 OK | Backend returns array, frontend expects paginated object | Handle both formats with |
| Tag filtering crashes | Backend returns tag objects | Update TypeScript types to match Pydantic schemas - see Tag Filtering |
| Pagination shows "NaN" | Optional priority field used in arithmetic without null check | Add null checks with defaults for optional fields - see Priority Sorting |
| Tags not saving to database | TaskCreate schema doesn't accept tags field | Use multi-step operation: create task, then assign tags - see Tag Assignment |
| Edit form fields blank | Uncontrolled components + field name mismatches + datetime format | Use controlled components, match field names, convert datetime - see Edit Form |
| 500 Error: timezone comparison | Comparing offset-naive and offset-aware datetimes | Normalize both to UTC before comparison - see Timezone Fix |
| Tag color validation fails | Frontend required color, backend allows optional | Make color optional in Zod schema, provide defaults - see Tag Color |
| Tag filter checkboxes broken | Backend returns | Convert IDs to strings for comparison - see Tag Filters |
references/troubleshooting.md@lru_cachekidreferences/jwks-approach.mdreferences/security-checklist.mdverify_jwks.pytest_jwt_verification.pyjwks-approach.mdsecurity-checklist.mdtroubleshooting.mdjwt_verification.pyauth_dependencies.pyapi_client.tsbetter_auth_migrations.py| Aspect | JWKS | Shared Secret |
|---|---|---|
| Security | ✅ Asymmetric (more secure) | ⚠️ Symmetric (less secure) |
| Scalability | ✅ Multiple backends | ⚠️ Secret must be shared |
| Production | ✅ Recommended | ⚠️ Development only |
| Complexity | Medium | Simple |