Auth0 SDK for React Native

5.5.0 · active · verified Tue Apr 21

Auth0 SDK for React Native provides a comprehensive toolkit for integrating Auth0 authentication and authorization into React Native applications, supporting iOS, Android, and React Native Web. It is currently on stable version 5.5.0 and maintains an active release cadence with frequent minor and patch updates to introduce new features (e.g., DPoP, MRRT, Custom Token Exchange) and fix issues. Key differentiators include full support for the React Native New Architecture and Expo 53+, abstracting complex OAuth/OIDC flows, and robust handling of token management, silent authentication, and user sessions. The library aims to simplify secure authentication implementation across diverse mobile platforms.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the Auth0 client, performing an interactive login via `webAuth.authorize()`, retrieving the access token, and clearing the session with `webAuth.clearSession()`.

import React, { useState } from 'react';
import { Button, Text, View, StyleSheet, Alert } from 'react-native';
import Auth0 from 'react-native-auth0';

// Ensure these are set in your .env or similar configuration
const AUTH0_DOMAIN = process.env.AUTH0_DOMAIN ?? 'YOUR_AUTH0_DOMAIN.auth0.com';
const AUTH0_CLIENT_ID = process.env.AUTH0_CLIENT_ID ?? 'YOUR_AUTH0_CLIENT_ID';
const AUTH0_AUDIENCE = process.env.AUTH0_AUDIENCE ?? 'YOUR_API_AUDIENCE'; // Optional

const auth0 = new Auth0({
  domain: AUTH0_DOMAIN,
  clientId: AUTH0_CLIENT_ID,
});

export default function Auth0Quickstart() {
  const [accessToken, setAccessToken] = useState<string | null>(null);

  const login = async () => {
    try {
      const credentials = await auth0.webAuth.authorize({
        scope: 'openid profile email offline_access',
        audience: AUTH0_AUDIENCE,
      });
      setAccessToken(credentials.accessToken);
      Alert.alert('Success', `Logged in! Token starts with: ${credentials.accessToken.substring(0, 10)}...`);
    } catch (e: any) {
      console.error('Login failed:', e);
      Alert.alert('Login Error', e.message || 'An unknown error occurred during login.');
    }
  };

  const logout = async () => {
    try {
      await auth0.webAuth.clearSession({});
      setAccessToken(null);
      Alert.alert('Success', 'Logged out.');
    } catch (e: any) {
      console.error('Logout failed:', e);
      Alert.alert('Logout Error', e.message || 'An unknown error occurred during logout.');
    }
  };

  return (
    <View style={styles.container}>
      <Text style={styles.header}>Auth0 React Native Integration</Text>
      {accessToken ? (
        <View>
          <Text style={styles.statusText}>You are logged in!</Text>
          <Button title="Log Out" onPress={logout} />
        </View>
      ) : (
        <View>
          <Text style={styles.statusText}>You are logged out.</Text>
          <Button title="Log In" onPress={login} />
        </View>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    padding: 20,
    backgroundColor: '#f5f5f5',
  },
  header: {
    fontSize: 24,
    fontWeight: 'bold',
    marginBottom: 40,
    color: '#333',
  },
  statusText: {
    fontSize: 18,
    marginBottom: 20,
    textAlign: 'center',
    color: '#555',
  },
});

view raw JSON →