Why Use Custom Refresh Routes?
Custom refresh routes provide several security benefits:- HttpOnly cookies: Refresh tokens are stored in HttpOnly cookies, preventing XSS attacks from accessing them
- Domain-specific: Cookies are tied to your domain, not a third-party domain
- Secure transmission: Configure cookies with
SecureandSameSiteflags for additional protection - Centralized control: Manage token refresh logic on your own backend
README.md:83
Cookie Configuration
Configure cookie options when initializingCrossmintAuth:
README.md:72
Cookie Options
When
true, makes the refresh token cookie inaccessible to client-side JavaScript. The JWT cookie will always be accessible to the client SDK.When
true, cookies are only sent over HTTPS connections. Always set to true in production.Controls when cookies are sent with cross-site requests:
Strict: Cookies are only sent in first-party contextsLax: Cookies are sent with top-level navigationsNone: Cookies are sent with all requests (requiressecure: true)
The domain for which the cookies are valid. Use a leading dot (e.g.,
.example.com) to include subdomains.cookies.ts:83
Setting Up a Custom Refresh Route
Configure CrossmintAuth with cookie options
Initialize Source:
CrossmintAuth with your desired cookie configuration:README.md:106Create the refresh endpoint
Implement the refresh route handler using Source:
handleCustomRefresh():README.md:92How Custom Refresh Works
When a token needs to be refreshed:Extract refresh token
The server extracts the refresh token from either the request body or cookies:Source:
CrossmintAuthServer.ts:89CrossmintAuthServer.ts:79
Setting Up a Custom Logout Route
When using HttpOnly cookies, logout must happen server-side:README.md:116
Error Handling
ThehandleCustomRefresh() method automatically handles errors:
- Returns a 401 Unauthorized response if the refresh token is invalid
- Clears authentication cookies on error
- Logs error details to the console
CrossmintAuthServer.ts:113
Complete Example
Here’s a complete example with custom refresh and logout routes:Best Practices
Always use secure cookies in production
Always use secure cookies in production
Use Strict SameSite policy when possible
Use Strict SameSite policy when possible
Use
sameSite: "Strict" for maximum protection against CSRF attacks, unless you need cross-site functionality:Configure domain for subdomain support
Configure domain for subdomain support
If your app uses subdomains, set the domain with a leading dot:
Handle refresh errors gracefully
Handle refresh errors gracefully
Always handle refresh errors by redirecting users to login:
Next Steps
Authentication
Learn more about authentication methods
Session Management
Understand session validation and token refresh