What is JWT (JSON Web Tokens)?
TL;DR
Compact URL-safe token format for securely transmitting user authentication and authorization data.
JSON Web Tokens (JWT) are compact, URL-safe tokens for securely transmitting information between parties. JWTs are commonly used for authentication and authorization in Frontend Accelerator, allowing stateless authentication without server-side sessions.
JWT Structure:
Header.Payload.Signature
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxMjMiLCJlbWFpbCI6InVzZXJAZXhhbXBsZS5jb20iLCJleHAiOjE3MzAwMDAwMDB9.signature
JWT usage:
import { SignJWT, jwtVerify } from 'jose';
const token = await new SignJWT({ userId: user.id })
.setProtectedHeader({ alg: 'HS256' })
.setExpirationTime('7d')
.sign(secret);
const { payload } = await jwtVerify(token, secret);
Key components:
- Header: Algorithm and token type
- Payload: Claims (user data, expiration)
- Signature: Cryptographic signature for verification
Key benefits:
- Stateless: No server-side session storage required
- Portable: Tokens work across multiple domains
- Secure: Cryptographically signed and tamper-proof
- Efficient: Reduces database queries for authentication
AI-Friendly: JWTs follow a well-defined standard that AI can implement correctly. AI can generate secure token creation, implement proper expiration handling, add refresh token logic, and ensure secure storage practices.