Session Management
Learn how to securely manage authentication tokens and maintain user sessions in native mobile and desktop applications.
Overview
Native applications manage their authenticated state directly on the device.
When a user successfully authenticates with Wristband, the application receives an access token, an ID token, and optionally a refresh token. These tokens allow the application to maintain the user's authenticated state and make authenticated API requests without requiring the user to log in again.
Native session management involves two distinct types of sessions:
- Application Session: Maintained locally by the native application using securely stored authentication tokens and user information.
- Wristband Authentication Session: Maintained by Wristband through a browser cookie associated with the user's tenant domain. This session allows Wristband to recognize previously authenticated users during subsequent login flows.
These sessions are managed independently. Clearing the application's local authentication state does not automatically terminate the user's Wristband authentication session.
Managing Sessions on Native Applications
After successful authentication, the native application must securely store the tokens and user information needed to maintain the user's authenticated state across application launches.
This typically includes:
- Access token
- Refresh token (if issued)
- Access token expiration time
- User and tenant information
Securely Storing Authentication Data
Native applications should use platform-provided secure storage mechanisms to protect sensitive authentication data, such as:
- iOS and macOS: Apple Keychain
- Android: Android Keystore-backed secure storage
- Windows: Windows platform credential-protection APIs
Access tokens can be kept in memory while the application is running. Refresh tokens and other sensitive authentication data that must persist between application launches should be stored using the device's secure storage mechanisms.
Avoid storing authentication tokens in plaintext files, application preferences, logs, or other unprotected storage locations.
Restoring the Application Session
When a user closes and reopens the native application, the application can restore their authenticated state using previously stored authentication information.
The following steps describe the session restoration process:
- Retrieve Stored Authentication Data: Load the previously stored tokens and relevant session information from the device's secure storage.
- Check Access Token Expiration: Determine whether the stored access token is still valid based on its expiration time.
- Refresh the Access Token if Needed: If the access token has expired and a refresh token is available, call Wristband's Token Endpoint to obtain a new access token.
- Restore Authenticated State: If a usable access token is available, restore the user's authenticated state and allow access to authenticated areas of the application.
- Require Reauthentication if Necessary: If no usable access token is available and the application cannot refresh it, using a refresh token, initiate a new authentication flow.
An expired access token does not necessarily mean the user's application session has ended. As long as a valid refresh token is available, the application can obtain a new access token without requiring the user to reauthenticate.
Clearing the Application Session on Logout
When a user logs out, your native application is responsible for clearing the locally stored authentication state to terminate the application session.
Unlike backend server integrations, which typically clear a session cookie, native applications must remove authentication data stored on the device.
The logout process should perform the following steps:
- Delete Stored Authentication Data: Remove access tokens, refresh tokens, and other sensitive session information from the device's secure storage.
- Clear In-Memory Authentication State: Remove cached tokens, user information, and other authentication data maintained in application memory.
- Reset the Application State: Clear any user-specific cached data as appropriate and return the user to an unauthenticated state.
Clearing the local application session does not automatically revoke tokens or terminate the separate authentication session managed by Wristband. For the complete logout flow, see the Native Logout Flow documentation.
Updated 19 days ago