How Provider Token Management Works
After a user completes OAuth authentication, you can retrieve and manage the provider’s tokens to interact with their external accounts:The Token Flow
- OAuth Completion: After the user successfully authenticates with an OAuth provider, they are redirected back to your application
-
Token Retrieval: Your frontend immediately calls
/signin/provider/{provider}/callback/tokenswith the user’s Nhost authentication token to retrieve the provider’s tokens - Secure Storage: The provider’s access token, refresh token, and expiration time are stored securely in your application
- Provider API Calls: Your application uses the provider’s access token to make API calls to the external service on behalf of the user
- Token Refresh: When the access token expires, your application uses the refresh token to obtain a new access token without requiring the user to re-authenticate
Retrieving Provider Tokens
After a successful OAuth sign-in, retrieve the provider’s session to access their tokens.- This method must be called immediately after the OAuth callback completes
- The provider session is stored temporarily and is cleared during this call
- Subsequent calls will fail without re-authenticating with the provider
- Requires the user to be authenticated with Nhost
Refreshing Provider Tokens
Provider access tokens typically expire after a short period (often 1 hour). Use the refresh token to obtain a new access token without user interaction.- Some providers (like Google) rotate refresh tokens, returning a new one in the response
- Always update your stored refresh token with the new value if provided
- Token refresh can fail if the user revokes access or the refresh token expires
- Handle these cases by prompting the user to re-authenticate
Best Practices
Immediate Retrieval - Call the tokens endpoint immediately after OAuth callback to prevent session loss Secure Storage - Store provider tokens in secure storage appropriate for your platform (encrypted storage for mobile, httpOnly cookies or secure localStorage for web) Proactive Refresh - Use theexpiresIn value to refresh tokens before they expire, preventing API call failures
Handle Rotation - Always update your stored refresh token when a new one is returned, as some providers invalidate old refresh tokens
Graceful Errors - Handle token refresh failures by prompting users to re-authenticate, as they may have revoked access through the provider’s settings
Token Isolation - Store tokens per provider and per user to support multiple OAuth connections