--- description: How to integrate with a React Native app --- # React Native SDK [![LLM | View as markdown](https://img.shields.io/badge/LLM-View%20as%20markdown-blue)](https://raw.githubusercontent.com/authgear/docs/refs/heads/main/get-started/native-mobile-app/react-native.md) This guide provides instructions on integrating Authgear with a React Native app. Supported platforms include: * React Native 0.60.0 or higher Follow this guide to add Authgear to your React Native app in 🕐 10 minutes. {% hint style="info" %} You can find the full code for the demo app for this tutorial in the Github repo [here](https://github.com/authgear/authgear-example-react-native). {% endhint %} {% hint style="info" %} React Native have opt-in support for the [New Architecture](https://reactnative.dev/docs/new-architecture-intro) since 0.68. Given that the New Architecture is still considered as unstable, we do not support it at the moment. {% endhint %} ## Video Guide for React Native {% embed url="https://www.youtube.com/watch?v=jCzqVrTzk_o" %} ## Setup Application in Authgear Signup for an Authgear Portal account in [https://portal.authgear.com/](https://portal.authgear.com/). Or you can use your self-deployed Authgear. From the Project listing, create a new Project or select an existing Project. After that, we will need to create an application in the project. ### **Step 1: Create an application in Authgear Portal** Go to **Applications** on the left menu bar.

authgear navigate to applications

You'll see the "**New Application**" page, or Click **⊕Add Application** in the top tool bar. Input the name of your application and select **Native App** as the application type. Click "**Save**". On the next screen, you will see a list of guides that can help you with setting up, click "**Next**" to continue. ![Create an application](<../../.gitbook/assets/authgear-new-app-native-2 (1).png>) ### **Step 2: Configure the application** In your IDE, define a custom URI scheme that will be used to redirect users back to your app after they have authenticated with Authgear. For example, in our example app, we will define the following URI scheme: ``` com.authgear.example.rn://host/path ``` For further instruction on setting up custom URI scheme in React Native, see [https://reactnative.dev/docs/linking](https://reactnative.dev/docs/linking) Now head back to Authgear Portal, and add the URI that you have defined (`com.authgear.example.rn://host/path` for this example) as an **Authorized Redirect URI**. Click "**Save**" and note the **Client ID** and **Endpoint** for your Authgear client application. You can also obtain it again from the **Applications** list later. ![set redirect URI](../../.gitbook/assets/authgear-uri-rn.png) ## Add User Authentication to React Native App using Authgear SDK In this section, we'll walk through the steps to create a new React Native app and use the Authgear SDK to add user authentication to the app. ### Step 1: Create a React Native app Run the following command to create a new React Native project: ```bash npx @react-native-community/cli init myapp cd myapp ``` For a more detailed guide on how to create a project and set up a development environment for React Native, follow the [official documentation of React Native](https://reactnative.dev/docs/getting-started). ### Step 2: Install the SDK Run the following commands from the root directory of your React Native project to install the Authgear SDK: ```bash npm install --exact @authgear/react-native (cd ios && pod install) ``` ### Step 3: Initialize Authgear In this step, we'll implement the code to initialize an instance of the Authgear SDK which we will be using to interact with the Authgear client application we created earlier. First, open the `App.tsx` file in your project then add the following import statements at the top: ```typescript import React, { useCallback, useEffect, useMemo, useState } from 'react'; import authgear, { Page, ReactNativeContainer, SessionState, SessionStateChangeReason } from "@authgear/react-native"; ``` Add the following code at the top inside the `App()` function in `App.tsx` to configure a new Authgear instance and set up a `delegate` that will help our app to know the current state of a user's session (whether they're logged in or not): ```typescript const [sessionState, setSessionState] = useState(() => { return authgear.sessionState; }); const loggedIn = sessionState === "AUTHENTICATED"; const delegate = useMemo(() => { const d = { onSessionStateChange: ( container: ReactNativeContainer, _reason: SessionStateChangeReason ) => { setSessionState(container.sessionState); }, sendWechatAuthRequest: () => {}, }; return d; }, [setSessionState]); useEffect(() => { authgear.delegate = delegate; return () => { authgear.delegate = undefined; }; }, [delegate]); const postConfigure = useCallback(async () => { const sessionState = authgear.sessionState; // if user has an existing session, call SDK fetchUserInfo method to get the user's info and refresh access token when necessary if (sessionState === "AUTHENTICATED") { await authgear.fetchUserInfo(); } }, []); useEffect(() => { const configure = async () => { try { await authgear .configure({ clientID: "", endpoint: "", }); await postConfigure(); } catch (error) { console.log("Error:" + error); } }; configure(); }, [postConfigure]); ``` Replace `` and `` with the Client ID and Endpoint for your Authgear client application. The above code includes a `postConfigure()` method that helps to get the true session state for a user that was previously logged in. ### Step 4: Add Login Button Replace the content for the `return` statement in the `App()` function inside the `App.tsx` file with the following: ```tsx return ( {!loggedIn ? Welcome