Building Secure Authentication Flows in React Native with Auth0
Authentication is more than a login screen. A mobile app needs a reliable way to authenticate users, keep tokens out of ordinary app storage, recover from expired sessions, and sign out cleanly.
This guide uses Auth0 Universal Login with a React Native app. It focuses on the boundaries and decisions that matter in a real project rather than building a custom password form.
The flow at a glance
- The app opens Auth0 Universal Login in the system authentication session.
- Auth0 authenticates the user and redirects back to the application.
- The app receives credentials and uses an access token to call an API.
- When necessary, the SDK's credentials manager obtains fresh credentials, subject to your tenant configuration.
- Logging out clears local credentials and ends the browser-based Auth0 session.
The ID token describes the authenticated user. The access token is intended for an API. Don't use an ID token as an API authorization token.
1. Configure Auth0
Create a Native Application in the Auth0 dashboard. Record its domain and client ID. Configure the callback and logout URLs for your app's native URL scheme as described by the Auth0 React Native SDK documentation.
If your backend validates Auth0-issued JWT access tokens, configure an Auth0 API and its audience identifier as well. Request that audience when logging in. The backend must validate issuer, audience, signature, expiry, and required permissions.
Do not embed a client secret in a mobile app: a native application is a public client.
2. Install the React Native SDK
npm install react-native-auth0
This package includes native code. For an Expo project, use a development build and follow the SDK's Expo configuration instructions; Expo Go alone cannot load arbitrary native modules. Add the matching callback URL scheme to your native configuration and verify both iOS and Android return to your app.
3. Sign in and get credentials
Wrap the app in Auth0Provider before calling useAuth0. Replace the domain and client ID with your Native Application settings. In this example, save the screen below as SignInScreen.tsx beside App.tsx:
import { Auth0Provider } from 'react-native-auth0';
import { SignInScreen } from './SignInScreen';
export default function App() {
return (
<Auth0Provider domain="YOUR_AUTH0_DOMAIN" clientId="YOUR_AUTH0_CLIENT_ID">
<SignInScreen />
</Auth0Provider>
);
}
import { useAuth0 } from 'react-native-auth0';
import { Button, View, Text } from 'react-native';
const AUDIENCE = 'https://api.example.com';
export function SignInScreen() {
const { authorize, user, error, isLoading } = useAuth0();
async function signIn() {
try {
await authorize({
audience: AUDIENCE,
scope: 'openid profile email offline_access',
});
} catch {
console.warn('Sign-in was not completed');
}
}
if (isLoading) return <Text>Loading…</Text>;
return (
<View>
<Text>{user ? `Hello ${user.name ?? 'there'}` : 'Sign in to continue'}</Text>
{error ? <Text>Authentication unavailable</Text> : null}
<Button title="Sign in" onPress={signIn} />
</View>
);
}
The offline_access scope requests a refresh token when your Auth0 application and API configuration permit it. Configure refresh-token rotation and expiration policies in your tenant; never assume a refresh token is perpetual.
4. Send an access token to your API
Get credentials at the point you need them rather than copying tokens into global state or AsyncStorage.
import { useAuth0 } from 'react-native-auth0';
export function useAccountApi() {
const { getCredentials } = useAuth0();
return async function loadAccount() {
const credentials = await getCredentials();
if (!credentials?.accessToken) {
throw new Error('Sign in again before requesting account data');
}
const response = await fetch('https://api.example.com/account', {
headers: {
Authorization: `Bearer ${credentials.accessToken}`,
},
});
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
return response.json();
};
}
SDK APIs and credential-refresh behavior can vary by version. Check the version you have installed and test expired-token recovery on devices.
5. Sign out deliberately
import { useAuth0 } from 'react-native-auth0';
export function useSignOut() {
const { clearSession, clearCredentials } = useAuth0();
return async () => {
await clearSession();
await clearCredentials();
};
}
clearSession involves browser/session behavior; clearCredentials removes locally managed credentials. Production apps should handle cancellation and errors so the UI never claims that sign-out completed when it did not.
Security checklist
- Use the system browser / Universal Login instead of collecting passwords in application screens.
- Use Authorization Code with PKCE, as supported by the SDK.
- Store refresh tokens only through platform-protected credential management.
- Use HTTPS for identity and API traffic; do not log tokens, authorization headers, or full credential objects.
- Keep access tokens short-lived and validate them on the server.
- Treat biometrics as a local unlock mechanism, not a substitute for API token validation.
- Test account switching, revoked refresh tokens, expired sessions, canceled login, and logout.
Further reading
Takeaway: Let the identity provider handle authentication, let the SDK handle native credential storage, and let your API enforce authorization. Those clear boundaries are more valuable than a clever login UI.