Overview
TheverifyAuth function is a generic authentication verification utility that works in any Node.js environment. It can accept either a raw JWT token string or a request object with cookies and headers.
Signature
Parameters
Either a JWT token string or a request object containing cookies and headersWhen passing an object:
cookies: Cookie object (e.g., from cookie-parser)headers: Headers object or Headers API
Configuration options for token verification
Returns
Promise that resolves to authentication context
Throws
- Throws an error if no token is found and
allowGuestisfalse - Throws an error if token verification fails and
allowGuestisfalse - When
allowGuest: true, returns guest context instead of throwing
Examples
With JWT Token String
With Request Object (Generic)
With Guest Support
With Required Scope
Custom Cookie Name
Token Extraction Order
When a request object is provided, tokens are extracted in this order:- Cookie (using
cookieNameoption, defaults tologto_authtoken) - Authorization header (Bearer token)
Verification Process
The function performs the following verification steps:- Fetches JWKS (JSON Web Key Set) from your Logto server
- Decodes JWT header to identify the signing key
- Verifies JWT signature using the public key
- Validates token claims:
- Issuer (
iss) matches Logto URL - Audience (
aud) matches provided audience - Token is not expired (
exp) - Token is valid (
nbf- not before) - Required scope is present (if specified)
- Issuer (
Caching
JWKS (signing keys) are cached for 5 minutes to reduce requests to the Logto server and improve performance.See Also
- createExpressAuthMiddleware - Express.js middleware
- verifyNextAuth - Next.js authentication helper
- useAuth - React hook for client-side authentication