Overview
TheverifyAuth function is a flexible authentication utility that works in any Node.js environment. It accepts either a raw JWT token string or a request object with cookies/headers.
Function Signature
Parameters
Either a JWT token string or a request object containing cookies and/or headers
Authentication configuration options
Return Value
User authentication context
Errors
Throws an error when:- No token found in request and
allowGuestis false - Invalid JWT format
- Token signature verification fails
- Token has expired
- Invalid issuer or audience
- Missing required scope
Usage Examples
With Raw Token String
Verify a JWT token directly:With Request Object
Extract token automatically from cookies or headers:AWS Lambda Function
Cloudflare Workers
GraphQL Resolver Context
tRPC Middleware
Fastify Plugin
Hono Middleware
Next.js Server Actions
Guest Mode
WhenallowGuest: true, the function returns guest context instead of throwing errors:
Error Handling
Without Guest Mode
With Guest Mode
Token Extraction Priority
When passing a request object, tokens are checked in this order:- Cookie:
cookies[cookieName](default:logto_authtoken) - Authorization Header:
headers.authorization(Bearer token)
Best Practices
Use Environment Variables
Use Environment Variables
Store Logto configuration in environment variables:
Handle Errors Gracefully
Handle Errors Gracefully
Always wrap calls in try-catch unless using
allowGuest:Validate isAuthenticated
Validate isAuthenticated
When using guest mode, always check authentication status:
Cache Configuration
Cache Configuration
Create a reusable configuration object:
Type Definitions
Related
Express Middleware
Ready-to-use Express middleware
Next.js Integration
Next.js-specific authentication