Top 5 Auth Solutions for React Native

Get to know 5 Auth Providers for React Native to get a head start with your Authentication flow

Viduni Wickramarachchi
Bits and Pieces

--

With the increasing threats out there, the stability of the authentication mechanism is vital for any application. As a result, today’s standard practice is to integrate an Auth Solution, which is hardened for security.

In this article, I’ll explore five such Auth solutions for React Native. In summary, the Auth solutions I have chosen are as follows.

  • Auth0
  • AWS Amplify and Cognito
  • Firebase
  • Azure AD B2C
  • Ping Identity

Let’s have a look at each of these solutions one by one.

💡 Pro Tip: To cut down on boilerplates, consider adopting a component-driven approach with tools like Bit.

Learn more:

1. Using Auth0 — Leaders in Pluggable Auth Solution Space

Auth0 is a flexible, easy-to-use solution for authentication and authorization for your React Native applications. As the industry leader, it comes with many features where you can configure both authentication and authorization.

Pros of using Auth0 for React Native

  • Has an SDK for React Native
  • User and Password support with verification and forgot password email workflow
  • Passwordless flow
  • Enterprise support
  • Integration with 20+ social providers
  • Active Directory support
  • Great documentation and guides especially for React Native

Configuring Auth0 to your React Native application requires a few steps. First of all, you would need the dependency installed. This can be done using the following command.

yarn add react-native-auth0

Detailed steps of configuring Auth0 for iOS and Android can be found here.

Choosing the right Auth flow for React Native

You can configure OpenID Connect authorization code flow with Proof Key for Code Exchange (aka PKCE) which better fits mobile apps.

How does the authorization flow work?

  • PKCE enables creating a secret (aka Code Verifier) by the calling application which is then verified by the authorization server.
  • The calling application creates a transform value (aka Code Challenge) of the Code Verifier.
  • The Code Challenge is sent over HTTPS to retrieve an authorization code.
  • Once the calling application receives the authorization code, it is sent back to /oauth/token along with the Code Verifier.
  • Next, the authorization server validates the Code Verifier and the Code Challenge.
  • If they are valid, the server sends an ID token and an access token back to the calling application. Authorization complete!

2. Using AWS Amplify and Cognito — Default for AWS and Cost-Effective

Authenticating your React Native application with Amplify is another easy authentication mechanism you can use. The Amplify Framework uses Amazon Cognito as the authentication provider.

Pros of using AWS Amplify and Cognito for React Native

  • Ability to intercept the token and add custom claims
  • Auth and API services provided by AWS are easily accessible via the aws-amplify package
  • aws-amplify-react-native package provides ready to use UI components for React Native
  • Better at security — Includes a “Remember Mobile” feature
  • Low cost — Cost-per-login pricing model (Free up to 50000 monthly active users)

Let’s look at how to configure AWS Amplify and Cognito to a React Native application.

How to configure AWS Amplify and Cognito in practice

  1. Create an authentication service
amplify add auth

Once this command is run, you will be asked three questions. I will choose the default configuration with Username and not configure any advanced setting. These will be the answers to the three questions respectively. Next, to deploy the service, you need to run the command below.

amplify push

That’s it. The authentication service is deployed and you use it now. If you want to view the deployed services, you can run amplify console and go to the Amplify Console.

2. Configure your application

First of all, we have to install the necessary dependencies to the React Native project.

yarn add aws-amplify amazon-cognito-identity-js @react-native-community/netinfo @react-native-async-storage/async-storage

Further, the pod dependencies for iOS also need to be installed.

npx pod-install

Next, the configuration file has to be imported and loaded in your app entry point, App.js .

import Amplify, { Auth } from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);

3. Enable sign-up, sign-in, and sign-out

The Amplify Framework has authentication UI components that you can utilize which will provide the entire authentication flow. In order to use these, you have to install some dependencies. This will vary based on whether you are using Expo or React Native CLI.

Expo:

yarn add aws-amplify-react-native

React Native CLI:

yarn add aws-amplify-react-nativenpx pod install

Next, you have to integrate the built-in UI components with the frontend. Open the App.js file and make the following changes.

  1. Import the withAuthenticator component
  2. Change the default export for withAuthenticator to be the HOC wrapping the main component.
import { withAuthenticator } from 'aws-amplify-react-native'.....export default withAuthenticator(App)

This concludes configuring the authentication flow with AWS Amplify and Cognito. If you are not happy with using pre-built UI components, you can call the authentication APIs manually as well. For more information about it, refer to this documentation.

3. Using Firebase — Works Best with Firebase Suit

Firebase is a Backend-as-a-Service (Baas) by Google which provides many products. A real-time database, authentication, cloud firestore, cloud functions, and crashlytics are among those.

Pros of using Firebase authentication for React Native

  • Built-in function to identify whether the user is logged in or not
  • Ability to use a custom token to register new users
  • Easy to use
  • Ability to use social providers for authentication
  • Flexible, drop-in UI — Uses best practices for authentication on mobile devices
  • Two pricing models to choose from — Spark plan (Fixed amount with a limit) and Pay-as-you-go plan (Pay per verification)

How does it work?

When the user enters the login credentials to the React Native application, it is sent to the Firebase Authentication SDK. Firebase’s backend services verify these credentials and return a response to the client.

There are two ways you can implement authentication with Firebase.

  • Using the FirebaseUI Auth (A drop-in, easy to use solution)
  • Using the Firebase Authentication SDK (Requires manual integration)

Firebase authentication supports the following.

  • Authentication using passwords
  • Authentication using phone numbers
  • Authentication via popular identity providers such as Google, Twitter, Facebook, etc.

In order to use this in your React Native application, you need to install the relevant dependencies by running the following commands.

# Install and setup the app module
yarn add @react-native-firebase/app

# Install the authentication module
yarn add @react-native-firebase/auth

# If you're developing your app using iOS
cd ios/ && pod install

For more information about integrating Firebase authentication with React Native, you can refer to this documentation.

4. Using Azure AD B2C — Easy to Integrate with Azure and AD Authentication

Azure Active Directory B2C (Azure AD B2C) is an identity management service that provides the basic Auth capabilities for third-party applications. It also supports React Native with the help of a couple of packages.

Pros of using Azure AD B2C for React Native

  • Availability of plug and play packages which reduces development and configuration time
  • Several pricing models to choose from based on your operations
  • Single sign-on compatibility
  • One identity for multiple applications
  • Better security with multi-factor authentication
  • Availability of different authentication settings such as self-service password reset, enabling smart lockout, etc.

Using Azure AD B2C with React Native

Authentication with Azure AD B2C can easily be done using a few plug-and-play packages.

  • react-native-azure-auth
  • react-native-ad-b2c
  • ad-b2c-react-native (Inspired by react-native-ad-b2c )

These packages will encapsulate any complex configurations required to integrate Azure Active Directory (AD) authentication into your React Native application.

The react-native-azure-auth package implements the Azure AD OAuth2 API. Detailed configurations for using this package can be referred to here.

The react-native-ad-b2c package is a React Native client library for Azure AD B2C. This is a modified version of the above library to suit Azure AD B2C authentication.

Before integrating Azure AD authentication to your React Native application, as a pre-requisite, you need to register your app via the Microsoft Azure Portal. You will also have to specify a Redirect URL. This is the location where Azure AD B2C is expected to post the token back once the authentication or authorization is complete. The React Native application is expected to fetch the token and validate it to proceed with the next steps for the user.

References for the plug and play packages mentioned here can be found below.

However, one point to note is that, if you need a lot of customization with authentication, Azure AD might not be the best solution. It lacks customization abilities.

5. Using Ping Identity — Good for Enterprise Solutions

Ping Identity provides identity security for enterprise applications. Many top-level companies such as Netflix, Chevron, HP, TechData use this in their applications.

Pros of using Ping Identity

  • Single Sign-On — One-click access to all your applications
  • Realtime access security for apps and APIs
  • Multi-factor authentication
  • Intelligent API to detect and block API cyberattacks using AI
  • Data governance to manage customer privacy and aid to meet regulatory compliance required
  • A directory to manage identity and profile data at large scales

You can use their authentication APIs (PingOne) to embed a user-friendly authentication mechanism to your React Native application. With this, you can configure authentication workflows, create access tokens, and also set up different authentication policies for each app if you have multiple applications. It supports SAML, OAuth 2, and OpenID Connect to authorize clients and access data. More information about the authentication APIs can be found here.

At a glance, it seems like you have to manually configure all of these and implement them in your application. However, there is an easier way to get this done using readily available NPM packages as well. One such package is pingone-sample-native-mobile . This uses OAuth 2.0 as the authentication mechanism via PingOne.

A detailed guide about setting up this package with your application can be found here.

Summary

In this article, I’ve discussed 5 authentication solutions for React Native applications. While there are many other solutions out there, I found these 5 solutions even a beginner for React Native can integrate.

All of these solutions have their own benefits when used in React Native applications. I have mentioned some of these in the relevant sections.

I hope this article gave you some insight into different solutions you can use in your future projects. Let me know your thoughts about authentication solutions you have used in your React Native applications as well.

Thanks for reading!

Build Great Design Systems and Micro Frontends

Take frontend development to the next level with independent components. Build and collaborate on component-driven apps to easily unlocks Micro Frontends, and to share components.

OSS Tools like Bit offer a great dev experience for building and composing independent components, and build solo or together with your team.

Learn more:

Give it a try →

An independently source-controlled & shared React “card” component

--

--