Skip to main content

Overview

The verifyAuth 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

tokenOrRequest
string | object
required
Either a JWT token string or a request object containing cookies and/or headers
options
VerifyAuthOptions
required
Authentication configuration options

Return Value

AuthContext
object
required
User authentication context

Errors

Throws an error when:
  • No token found in request and allowGuest is 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

When allowGuest: 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:
  1. Cookie: cookies[cookieName] (default: logto_authtoken)
  2. Authorization Header: headers.authorization (Bearer token)

Best Practices

Store Logto configuration in environment variables:
Always wrap calls in try-catch unless using allowGuest:
When using guest mode, always check authentication status:
Create a reusable configuration object:

Type Definitions

Express Middleware

Ready-to-use Express middleware

Next.js Integration

Next.js-specific authentication