handleAuth creates a route handler function that processes OAuth callback requests from WorkOS AuthKit. It exchanges authorization codes for user sessions and manages session cookies.
Usage
Signature
Parameters
Configuration options for the authentication callback handler.
Returns
Returns a route handler function that accepts aNextRequest and returns a Promise<Response>.
Examples
Basic callback route
Custom redirect path
Persisting OAuth tokens
Handling custom state
Docker deployments
Custom error handling
Notes
- The callback route URL must match the redirect URI configured in your WorkOS dashboard
- The callback route URL must also match the
WORKOS_REDIRECT_URIenvironment variable authenticationMethodis only available during the initial authentication callback, not on session refreshes- When using custom state, remember to serialize complex data with
JSON.stringify()when creating auth URLs - The
baseURLoption is particularly useful for preview deployments and containerized environments