Session methods
TheCrossmintAuth class provides methods for managing user sessions, handling token refresh, and logging out users.
getSession()
Retrieves and validates the current user session. If the JWT is expired, automatically refreshes it using the refresh token.Parameters
Either an HTTP request object or authentication material.GenericRequest: Node.js
IncomingMessage or Web API Request. The method will extract authentication tokens from cookies.AuthMaterialBasic: Object with authentication tokens.The HTTP response object (Node.js
ServerResponse or Web API Response). Required if you want refreshed tokens to be automatically stored in cookies.Returns
Type:Promise<AuthSession>
Returns a session object containing:
The JWT access token (refreshed if expired).
The refresh token object.
The user’s unique identifier.
Examples
With request and response objects
With authentication material
Node.js Express
Error handling
ThrowsCrossmintAuthenticationError if:
- No authentication material is found
- Refresh token is missing
- Token refresh fails
- JWT verification fails
response object is provided, the method automatically calls logout() to clear cookies when authentication fails.
handleCustomRefresh()
Handles token refresh requests from clients. Accepts refresh tokens from either the request body or cookies, refreshes the authentication material, and stores it in cookies.Parameters
The HTTP request object (Node.js
IncomingMessage or Web API Request).The HTTP response object (Node.js
ServerResponse or Web API Response).Returns
Type:Promise<GenericResponse>
Returns a response object with:
- Refreshed authentication material in the response body
- Updated authentication cookies (via
Set-Cookieheaders) - Status 401 with error message if refresh fails
Example implementation
Next.js API route
Express.js
Request body format
The method accepts an optional refresh token in the request body:Response format
Success (200):Notes
This method automatically handles both Node.js and Web API request/response objects, adapting its behavior based on the runtime environment.
logout()
Logs out a user by clearing authentication cookies and optionally calling the Crossmint logout endpoint.Parameters
The HTTP request object. Used to extract the refresh token for calling the logout endpoint.
The HTTP response object. If not provided, creates a new
Response object.Returns
Type:Promise<GenericResponse>
Returns a response object with cleared authentication cookies (empty values with expired dates).
Examples
Next.js API route
Express.js
Client-side redirect after logout
Behavior
The method performs the following actions:- Attempts to call the Crossmint logout endpoint with the refresh token (if available)
- Clears authentication cookies by setting them to empty values with expired dates
- Returns the response object with updated headers
The method always clears cookies, even if calling the Crossmint logout endpoint fails. This ensures users can log out even if the server is unreachable.