React Native Authentication: Secure Storage, Biometrics, and Refresh Tokens
A login screen is only one part of authentication. A production mobile app also needs to protect credentials on the device, recover from expired access tokens, and make sign-out predictable.
This guide focuses on an Expo/React Native app using a standards-based identity provider (such as Auth0 or another OAuth 2.0/OpenID Connect provider). The same session-management principles apply to bare React Native projects.
Start with a clear token model
| Item | Purpose | Suggested handling |
|---|---|---|
| Access token | Authorizes API requests | Keep in memory where practical; send only to the intended API over HTTPS |
| Refresh token | Requests new access tokens | Store in OS-backed secure storage; never log it |
| ID token | Contains identity claims for the client | Don't use it as an API access token |
| Biometric unlock | Confirms the person using the device | Gate access to an existing local session; it is not a replacement for server authentication |
Prefer Authorization Code Flow with PKCE through the system browser or an identity-provider SDK. Avoid embedded web views for collecting passwords or implementing a custom password grant.
Store sensitive tokens in SecureStore
Install the Expo package:
npx expo install expo-secure-store
A small storage module keeps sensitive token access in one place:
// auth/tokenStorage.ts
import * as SecureStore from 'expo-secure-store';
const REFRESH_TOKEN_KEY = 'auth.refreshToken';
export async function saveRefreshToken(token: string): Promise<void> {
await SecureStore.setItemAsync(REFRESH_TOKEN_KEY, token);
}
export async function readRefreshToken(): Promise<string | null> {
return SecureStore.getItemAsync(REFRESH_TOKEN_KEY);
}
export async function clearRefreshToken(): Promise<void> {
await SecureStore.deleteItemAsync(REFRESH_TOKEN_KEY);
}
expo-secure-store uses platform security facilities, but it is not a universal backup or persistence guarantee. On iOS, keychain behavior after reinstall can differ from Android, and Android backup restoration requires care. Treat a missing or unreadable token as a signed-out session, not as an application error.
Never place refresh tokens in AsyncStorage, Redux persistence, query caches, analytics properties, exception messages, or debug logs.
Use biometric authentication as a local gate
If you want Face ID, Touch ID, or Android biometrics before revealing an already-authenticated session, you can use expo-local-authentication:
npx expo install expo-local-authentication
import * as LocalAuthentication from 'expo-local-authentication';
export async function unlockSession(): Promise<boolean> {
const hasHardware = await LocalAuthentication.hasHardwareAsync();
const isEnrolled = await LocalAuthentication.isEnrolledAsync();
if (!hasHardware || !isEnrolled) {
return false;
}
const result = await LocalAuthentication.authenticateAsync({
promptMessage: 'Unlock your session',
disableDeviceFallback: false,
});
return result.success;
}
Biometric success does not issue a new OAuth token. It just tells your app that the local device-user check succeeded. Your identity provider still determines whether the server-side session or refresh token is valid.
For iOS Face ID testing, use a development build rather than assuming Expo Go has the same permission behavior as your production binary. Configure the relevant Face ID usage description before shipping.
You can also explore SecureStore's requireAuthentication option for access to a particular secret, but test its platform-specific behavior and what happens when biometric enrollment changes. Don't blindly enable it on every stored value.
Restore a session without showing a login screen every launch
A typical startup flow is:
- Read the refresh token from secure storage.
- If none exists, show the signed-out state.
- If your app requires local unlock, perform that check.
- Ask your identity provider's supported SDK/token endpoint for new credentials.
- Keep the new access token in memory and securely replace the refresh token if one is returned.
- On
invalid_grantor a revoked session, clear local credentials and show login.
The exact refresh request is identity-provider specific. Prefer the provider SDK's credential manager when it securely handles storage and refresh for you; do not build a second competing token store.
Handle token expiry in your API client
With Axios 1.x installed (npm install axios@^1), attach the currently valid access token just before a request:
import axios from 'axios';
const API_ORIGIN = 'https://api.example.com';
export const api = axios.create({
baseURL: API_ORIGIN,
timeout: 15000,
});
let accessToken: string | null = null;
export function setAccessToken(token: string | null) {
accessToken = token;
}
api.interceptors.request.use((config) => {
const requestUrl = new URL(api.getUri(config));
if (requestUrl.origin !== API_ORIGIN) {
throw new Error('This client only supports the configured API origin');
}
if (accessToken) {
config.headers.set('Authorization', `Bearer ${accessToken}`);
}
return config;
});
A real app also needs an expiry/401 strategy. Refresh once for concurrent failures (a single-flight refresh), retry a request at most once, and avoid retrying authorization failures forever. Make sure the refreshed token is sent only to your configured API origin.
Do not refresh a token for every request; reuse a valid access token until it is close to expiry. Refresh token rotation depends on your identity-provider configuration, and some providers return a new refresh token on refresh while others do not.
Sign out deliberately
A complete sign-out flow should clear the in-memory access token, remove locally stored credentials, clear user-specific cached API data, and invoke the provider's logout/revocation flow when supported. Merely navigating to the login screen is not sufficient.
Production checklist
- Use HTTPS for token exchange and API calls; never log tokens.
- Validate access tokens at the API using issuer, audience, signature, expiry, and applicable claims.
- Handle offline startup and token refresh failures without leaving the UI stuck.
- Consider app screenshots, notification previews, and background transitions when displaying sensitive information.
- Treat biometrics as a device unlock feature unless you have implemented a separate cryptographic authentication protocol.
- Test account switching, token revocation, reinstall, device biometric changes, and logout.
Authentication should be boring in production: a predictable set of states, small sensitive-data surfaces, and a clear route back to login when the session is no longer valid.