Overview
When guest mode is enabled:- Unauthenticated users automatically get a unique guest ID
- Guest IDs are stored in the
guest_logto_authtokencookie for persistence - Your app can track guest users across sessions
- Guest users can later sign in without losing their data
Frontend Setup
Use theuseAuth hook with the guest middleware:
Middleware Options
TheuseAuth hook supports different middleware modes:
'auth': Require authentication (redirect if not logged in)'guest': Allow both authenticated and guest users
URL to redirect to when authentication is required (used with
middleware: 'auth')Route Protection with Auth Middleware
Server Setup
Enable guest mode in your server authentication middleware:Express.js
Next.js API Routes
Auth Context Structure
When guest mode is enabled, the auth context includes additional fields:How Guest IDs Work
1
Guest ID Generation
When a user visits your site without authentication, logto-authkit automatically generates a unique guest ID using browser fingerprinting.
2
Persistence
The guest ID is stored in the browser’s
guest_logto_authtoken cookie, ensuring the same ID is used across sessions.3
Server Verification
When
allowGuest: true is set, the server helpers can return a guest auth context when no valid JWT token is available, using the guest cookie to persist the guest ID.4
Upgrade to Authenticated
When a guest user signs in, they receive a proper JWT token. You can associate their previous guest activity with their authenticated account using the guest ID.
Use Cases
Shopping Cart
Allow users to add items to cart before signing in, then associate the cart with their account after login.
Content Personalization
Track guest preferences and browsing history, then merge with their profile when they sign in.
Analytics
Track user behavior for both authenticated and guest users with a consistent identifier.
Gradual Onboarding
Let users explore your app as guests, then prompt them to sign in when they need advanced features.
Example: Tracking Guest to User Conversion
Configuration Options
Enable guest mode in server middleware
Custom cookie name for both JWT tokens and guest IDs
Next Steps
Server Authentication
Learn more about server authentication
useAuth Hook
Explore all useAuth hook options