Skip to main content

Integrate Authentication into React Native

This guide shows how to create a simple React application and secure it with authentication powered by Ory. You can use this guide with both Ory Network and self-hosted Ory software.

This guide is perfect for you if:

A screenshot of the demo app developed in this guide showing first the login screen and then the post-login screen, containing relevant identity information

info

You can find the code of the sample application here. The application is also available to download from the Apple App Store.

Clone the sample app repository

Start with cloning the repository with the sample application. Run:

# Clone using SSH
git clone git@github.com:ory/kratos-selfservice-ui-react-native.git

# Clone using HTTPS
git clone https://github.com/ory/kratos-selfservice-ui-react-native.git

Prepare environment

To run the sample application, you must set up a React Native development environment and install mobile device emulators:

Run the application locally

Follow these steps to run the app locally in an emulated iOS environment:

  1. Enter the directory and install all dependencies:

    # Change directory
    cd kratos-selfservice-ui-react-native

    # Install dependencies
    npm i
  2. Run npm run ios to start the iOS simulator and run the application.

    tip

    You can also use these commands:

    • npm start opens a dashboard where you can choose to run the app in iOS or Android simulated environments.
    • npm run android runs the app in the Android environment.
  3. When the simulator starts, the Expo Go application opens on the simulated iOS device. If this doesn't happen, open the app manually.

  4. In the Expo Go application, click the + icon in the top-right corner.

  5. Enter the project URL provided by Metro:

    npm run ios

    >@ory/expo-login-registration-template@v0.5.4-alpha.1.pre.5 ios
    >expo start --ios

    Starting project at /Users/ory/Desktop/kratos-selfservice-ui-react-native
    Developer tools running on http://localhost:19002
    Starting Metro Bundler

    # This is the exact URL you must provide in the simulator. Starts with 'exp://'.
    Opening exp://192.168.1.144:19000 on iPhone 13 mini

By default, the application uses the "Playground" project, which is a public demo environment available to all Ory users. When using the application this way, make sure to anonymize any data you send to Ory!

Connect to your project

Instead of using the public playground project, you can connect the application directly to your project and its admin APIs. Follow these steps:

  1. Go to Project settingsOverview in the Ory Console, and copy the URL from the API Endpoints field.

  2. Open the app.config.js file to configure the application.

  3. Find the KRATOS_URL variable and replace the playground project URL with the SDK URL you copied from your project.

    app.config.js
    export default (parent = {}) => {
    // We gracefully destruct these parameters to avoid "undefined" errors:
    const { config = {} } = parent
    const { env = {} } = process || {}

    const {
    // This is the URL of your deployment. In our case we use the Ory Demo
    // environment
    KRATOS_URL = "https://playground.projects.oryapis.com",

    // We use sentry.io for error tracing. This helps us identify errors
    // in the distributed packages. You can remove this.
    SENTRY_DSN = "https://8be94c41dbe34ce1b244935c68165eab@o481709.ingest.sentry.io/5530799",
    } = env

    return {
    ...config,
    extra: {
    kratosUrl: KRATOS_URL,
    sentryDsn: SENTRY_DSN,
    },
    }
    }

Understanding the Implementation

With the application live, running, and connected to your project, let's have a closer look at the code to understand the implementation.

React navigation with authentication session

The entry point for the app is App.tsx. The component loads fonts, sets up the views, but most importantly, it defines the structure of the application - including the navigation:

App.tsx
// ...
export default function App() {
const [robotoLoaded] = useFontsRoboto({ Roboto_400Regular })
const [rubikLoaded] = useFontsRubik({
Rubik_300Light,
Rubik_400Regular,
Rubik_500Medium,
})

const hydratedTheme = {
...theme,
regularFont300: rubikLoaded ? "Rubik_300Light" : "Arial",
regularFont400: rubikLoaded ? "Rubik_400Regular" : "Arial",
regularFont500: rubikLoaded ? "Rubik_500Medium" : "Arial",
codeFont400: robotoLoaded ? "Roboto_400Regular" : "Arial",
platform: "react-native",
}

return (
<ThemeProvider theme={hydratedTheme}>
<NativeThemeProvider theme={hydratedTheme}>
<SafeAreaProvider>
<SafeAreaView
edges={["top", "left", "right"]}
style={{
flex: 1,
backgroundColor: theme.grey5,
}}
>
<ProjectProvider>
<AuthProvider>
<ErrorBoundary>
<Navigation />
<ForkMe />
</ErrorBoundary>
</AuthProvider>
</ProjectProvider>
</SafeAreaView>
</SafeAreaProvider>
</NativeThemeProvider>
</ThemeProvider>
)
}

The AuthProvider component

The <AuthProvider> component is the main point of the integration as it adds authentication and login context to the React Native component tree:

src/components/AuthProvider.tsx
// ...
export default function AuthContextProvider({ children }: AuthContextProps) {
const { sdk } = useContext(ProjectContext)
const [sessionContext, setSessionContext] = useState<
SessionContext | undefined
>(undefined)

// Fetches the authentication session.
useEffect(() => {
getAuthenticatedSession().then(syncSession)
}, [])

const syncSession = async (auth: { session_token?: string } | null) => {
if (!auth?.session_token) {
return setAuth(null)
}

try {
const { data: session } = await sdk
// whoami() returns the session belonging to the session_token:
.toSession({ xSessionToken: auth.session_token })

// This means that the session is still valid! The user is logged in.
//
// Here you could print the user's email using e.g.:
//
// console.log(session.identity.traits.email)
setSessionContext({ session, session_token: auth.session_token })
} catch (err: any) {
if (err.response?.status === 401) {
// The user is no longer logged in (hence 401)
// console.log('Session is not authenticated:', err)
} else {
// A network or some other error occurred
console.error(err)
}

// Remove the session / log the user out.
setSessionContext(null)
}
}

const setAuth = (session: SessionContext) => {
if (!session) {
return killAuthenticatedSession().then(() => setSessionContext(session))
}

setAuthenticatedSession(session).then(() => syncSession(session))
}

if (sessionContext === undefined) {
return null
}

return (
<AuthContext.Provider
value={{
// The session information
session: sessionContext?.session,
sessionToken: sessionContext?.session_token,

// Is true when the user has a session
isAuthenticated: Boolean(sessionContext?.session_token),

// Fetches the session from the server
syncSession: () => getAuthenticatedSession().then(syncSession),

// Allows to override the session
setSession: setAuth,

// Is true if we have fetched the session.
didFetch: true,
}}
>
{children}
</AuthContext.Provider>
)
}

Helper methods

The helper methods in src/helpers/auth.tsx are simple wrappers around the Expo SecureStore. To make them work in the web environment, @react-native-community/async-storage is used as a fallback:

src/helpers/auth.tsx

That's all it takes to make the magic happen! Everything else is handled by the Ory Session Token.

With this setup in place, the application can store and refresh the user session. Additionally, this allows the app to verify if the user session is still active in the navigation and shows the dashboard or login/registration screens accordingly:

src/components/Navigation.tsx
// ...
export default () => {
// import { AuthContext } from './AuthProvider'
const { isAuthenticated } = useContext(AuthContext)
return (
<KeyboardAvoidingView
style={{ flex: 1 }}
behavior={Platform.OS == "ios" ? "padding" : "height"}
>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<NavigationContainer linking={linking}>
<Stack.Navigator
screenOptions={{
headerShown: isAuthenticated,
}}
>
<Stack.Screen name="Home" component={Home} options={options} />
<Stack.Screen
name="Settings"
component={Settings}
options={options}
/>
<Stack.Screen name="Registration" component={Registration} />
<Stack.Screen name="Login" component={Login} initialParams={{}} />
<Stack.Screen name="Verification" component={Verification} />
<Stack.Screen name="Callback" component={Callback} />
<Stack.Screen name="Recovery" component={Recovery} />
</Stack.Navigator>
</NavigationContainer>
</TouchableWithoutFeedback>
<View data-testid={"flash-message"}>
<FlashMessage position="top" floating />
</View>
</KeyboardAvoidingView>
)
}

React Native authentication screens

To avoid writing a form renderer for every component - including styling - the app uses form rendering abstracted into separate own components, which you can find in src/components/Form.

Home component

The Home component receives the user's Session and displays all relevant information.

src/components/Routes/Home.tsx

User settings component

The User Settings component performs a User Settings API Flow.

src/components/Routes/Settings.tsx

Feedback