Home > Web Front-end > JS Tutorial > Build a React Native Application & Authenticate with OAuth 2.0

Build a React Native Application & Authenticate with OAuth 2.0

Lisa Kudrow
Release: 2025-02-15 09:47:11
Original
954 people have browsed it

Easy to integrate the authentication capabilities of React Native apps with Okta and OpenID Connect (OIDC) without building them yourself. OIDC allows you to authenticate the Okta API directly, and this article will demonstrate how to implement this feature in a React Native application. We will learn how to redirect login users through OIDC using the AppAuth library.

React Native is an efficient framework. Unlike Ionic and other hybrid mobile frameworks, it allows you to build native mobile applications using web technologies (React and JavaScript). It doesn't involve a browser or WebView, so developing a mobile app with React Native is similar to using a native SDK where you will do all the tests on your emulator or device. Not as testable as Ionic does in the browser. This can bring benefits because you don't have to write code that runs on your browser and on your device separately.

If you check out Google Trends, you will find that React Native is even more popular in native development than Android and iOS!

Build a React Native Application & Authenticate with OAuth 2.0

This article will guide you on how to develop applications using the latest version of React Native. At the time of writing, the latest versions are React 16.2.0 and React Native 0.54.0. We will create a new app, add AppAuth for authentication, use Okta for authentication, and run it on iOS and Android.

AppAuth is a native application client SDK for authentication and authorization of end users using OAuth 2.0 and OpenID Connect. It is suitable for iOS, macOS, Android and native JS environments, implementing modern security and usability best practices for native app authentication and authorization.

Create React Native App

React has a command line tool (CLI) called create-react-app that can be used to create new React apps. React Native also has a similar tool called Create React Native App. Before installing it, make sure Node v6 or higher is installed.

Install create-react-native-app and create a new project called okta-rn:

<code>npm install -g create-react-native-app
create-react-native-app okta-rn
cd okta-rn
npm start</code>
Copy after login
Copy after login
Copy after login

After running these commands, your terminal will prompt you with some options:

<code>...(省略终端输出,与原文相同)</code>
Copy after login
Copy after login

If you are using a Mac, press i to open the iOS emulator. You will be prompted to install/open Expo, and then render the rendered App.js.

Build a React Native Application & Authenticate with OAuth 2.0
Build a React Native Application & Authenticate with OAuth 2.0

If you are using Windows or Linux, it is recommended to try using an Android emulator or your Android device (if you have one). If it doesn't work, don't worry, we'll cover how to fix this later.

Tip: You can use TypeScript instead of JavaScript in React Native apps using Microsoft's TypeScript React Native Starter. If you decide to use this approach, it is recommended that you follow the steps to convert your application after this tutorial is completed.

React Native and OAuth 2.0

In this case, I will use React Native App Auth, a library created by Formidable. There are three reasons why I use this library: 1) They provide a great example that I can get it running in just a few minutes; 2) it uses AppAuth (a mature OAuth client implementation); 3) I can't Make any other method run.

  • I tried react-native-oauth but found that it requires using an existing provider before adding a new provider. I just want to use Okta as a provider. In addition, its numerous problems and requests also send warning signals.
  • I've tried react-native-simple-auth but have problems trying to get the deprecated Navigator component to work with the latest React Native version.
  • I've tried this React Native OAuth 2 tutorial but also had problems redirecting back to my app.

Create native apps in Okta

Before adding AppAuth to your React Native app, you need an app to authorize. If you don't have a free Okta developer account yet, sign up for one now!

Login your Okta developer account and navigate to Applications > Add Application. Click Native, and then click Next. Name the application (for example, React Native), select Refresh Token as the authorization type, and the default Authorization Code. Copy the login redirect URI (for example, com.oktapreview.dev-158606:/callback) and save it to a location. This value is required when configuring the application.

Tap Done and you should see a client ID on the next screen. Copy and save this value.

Add React Native AppAuth for authentication

You need to "pop up" the native configuration of the app, which is usually hidden by the create-react-native-app.

<code>npm install -g create-react-native-app
create-react-native-app okta-rn
cd okta-rn
npm start</code>
Copy after login
Copy after login
Copy after login

When the system prompts you to answer a question, please use the following answer:

Question Answer

How do you want to pop up from create-react-native-app? React Native What content should your app display on the user's homepage? Okta RN What should your Android Studio and Xcode projects be named? OktaRN

To install React Native App Auth, run the following command:

<code>npm install -g create-react-native-app
create-react-native-app okta-rn
cd okta-rn
npm start</code>
Copy after login
Copy after login
Copy after login

After running these commands, you must configure a native iOS project. For convenience, I've copied the following steps.

iOS settings

React Native App Auth depends on AppAuth-ios, so you have to configure it as a dependency. The easiest way is to use CocoaPods. To install CocoaPods, run the following command:

<code>...(省略终端输出,与原文相同)</code>
Copy after login
Copy after login

Create a Podfile in the project's ios directory, which specifies AppAuth-ios as the dependency. Make sure that OktaRN matches the application name specified when running npm run eject.

<code>npm run eject</code>
Copy after login

Then run pod install from the ios directory. The first run can take a while, even with fast connections. Now is a good time to have coffee or whiskey! ?

Open your project in Xcode by running open OktaRN.xcworkspace from the ios directory.

If you plan to support iOS 10 and earlier, you need to define the supported redirect URL scheme in ios/OktaRN/Info.plist as shown below:

<code>npm i react-native-app-auth@2.2.0
npm i
react-native link</code>
Copy after login

(The following content is the same as the original text, the duplicate parts are omitted, and only necessary modifications and explanations are retained)

... (The rest is the same as the original text, omitting duplicate code and description)

The above is the detailed content of Build a React Native Application & Authenticate with OAuth 2.0. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template