Skip to main content
While authentication routes are handled automatically by the middleware, you can customize the authentication flow by intercepting auth routes or using hooks.

Customization Approaches

There are two main ways to customize authentication handlers:
  1. Run custom code before auth handlers - Intercept auth routes in middleware
  2. Run code after authentication - Use the onCallback hook
Additional customization options include:
  • Login parameters via query parameters or static configuration
  • Session data modification using the beforeSessionSaved hook
  • Logout redirects using query parameters
When customizing auth handlers, always validate user inputs (especially redirect URLs) to prevent security vulnerabilities like open redirects. Use relative URLs when possible and implement proper input sanitization.

Running Custom Code Before Auth Handlers

Intercept authentication routes in your middleware to add custom logic before the SDK processes them.
middleware.ts

Use Cases

1. Force specific authentication parameters:
2. Logging and analytics:
3. Rate limiting:
4. Custom validation:

Running Code After Callback

Use the onCallback hook to run custom logic after authentication succeeds.

Using the onCallback Hook

lib/auth0.ts

Hook Parameters

Common Use Cases

1. Create user record:
2. Custom redirect logic:
3. Enrich session with database data:
4. Audit logging:
5. Error handling:

Modifying Session Before Save

Use the beforeSessionSaved hook to modify session data before it’s persisted.
lib/auth0.ts

Common Use Cases

1. Add custom claims:
2. Filter sensitive data:
3. Add timestamps:

Combining Customizations

You can combine multiple customization approaches:
lib/auth0.ts

Best Practices

  • Validate all user inputs to prevent security vulnerabilities
  • Keep hooks fast - Avoid long-running operations that slow down authentication
  • Handle errors gracefully - Always catch and log errors in hooks
  • Don’t store sensitive data in sessions unless necessary
  • Use TypeScript for type safety when modifying sessions
  • Test thoroughly - Test all customizations in development before deploying

Security Considerations

  • Validate redirect URLs to prevent open redirect attacks
  • Sanitize user input before using it in database queries or URLs
  • Use allowlists for acceptable values (e.g., connection names)
  • Log security events for audit trails
  • Never expose secrets in logs or error messages

Troubleshooting

Hook not being called

If your hooks aren’t executing:
  • Ensure hooks are defined in the Auth0Client constructor
  • Check for errors in the hook function (use try-catch)
  • Verify the auth flow is completing successfully

Redirect not working

If custom redirects aren’t working:
  • Ensure the returnTo URL is registered in Auth0 Allowed Callback URLs
  • Check that you’re returning the correct format from onCallback
  • Verify the URL is properly encoded

Session modifications not persisting

If session changes aren’t saved:
  • Use beforeSessionSaved, not onCallback, for session modifications
  • Ensure you’re returning the modified session object
  • Check that the session size doesn’t exceed cookie limits (4KB)