Overview
logto-authkit provides flexible server authentication utilities that work with Express.js, Next.js, and any Node.js environment. All authentication functions verify JWT tokens issued by your Logto server and provide a consistentAuthContext object.
Key Features
Express Middleware
Ready-to-use middleware for Express.js applications
Next.js Integration
Server-side authentication for Next.js API routes and middleware
Generic Usage
Flexible verifyAuth function for any Node.js environment
Guest Mode
Optional guest mode for unauthenticated users
Authentication Flow
All server authentication functions follow this flow:- Token Extraction: Checks for JWT token in cookies (default:
logto_authtoken) orAuthorizationheader - JWKS Fetching: Retrieves public keys from your Logto server (with 5-minute caching)
- Signature Verification: Verifies JWT signature using the appropriate public key
- Claims Validation: Validates issuer, audience, expiration, and required scopes
- Context Creation: Returns an
AuthContextobject with user information
AuthContext Object
All authentication functions return or set anAuthContext object:
Configuration Options
All authentication functions accept aVerifyAuthOptions object:
Your Logto server URL (e.g.,
https://your-tenant.logto.app)The API resource identifier registered in Logto
Name of the cookie containing the JWT token
Optional scope that must be present in the token
Enable guest mode for unauthenticated users. When enabled, failed authentication returns a guest context instead of throwing an error.
Guest Mode
WhenallowGuest is enabled, the authentication functions handle unauthenticated users gracefully:
- No token found: Returns guest context with generated
guestId - Invalid token: Falls back to guest context
- Guest ID stored in cookie:
guest_logto_authtoken(auto-generated UUID)
Token Sources
Authentication tokens can be provided in two ways (checked in order):1. Cookie (Recommended)
Set by the frontend authentication SDK:2. Authorization Header
Useful for API clients and mobile apps:Error Handling
Authentication functions throw errors in these scenarios:- No token found: When
allowGuestis disabled and no token is present - Invalid JWT format: Malformed token structure
- Signature verification failed: Token signature doesn’t match public key
- Token expired: Token’s
expclaim is in the past - Invalid issuer: Token wasn’t issued by your Logto server
- Invalid audience: Token’s
audclaim doesn’t match your API resource - Missing scope: Required scope not present in token
Security Features
JWKS Caching
JWKS Caching
Public keys are cached for 5 minutes to reduce load on your Logto server while maintaining security. The cache is automatically refreshed when expired.
Automatic Cookie Parsing
Automatic Cookie Parsing
Comprehensive Validation
Comprehensive Validation
All JWT claims are validated including issuer, audience, expiration (
exp), not-before (nbf), and custom scopes.Multiple Token Sources
Multiple Token Sources
Supports both cookie-based (for web apps) and header-based (for APIs) authentication in the same endpoint.
Next Steps
Express Setup
Add middleware to Express routes
Next.js Setup
Protect Next.js API routes
Generic Usage
Use in any Node.js environment