This tutorial shows how to use PlusAuth with React Native Application. If you do not have a PlusAuth account, register from here.
Create PlusAuth Client
After you sign up or log in to PlusAuth, you need to create a client to get the necessary configuration keys in the dashboard. Go to Clients and create a client with the type of Native Application
Configure Client
Get Client Properties
You will need your Client Id
for interacting with PlusAuth. You can retrieve them from the created client's details.
Configure Redirect and Logout URIs
When PlusAuth authenticates a user, it needs a URI to redirect back. That URI must be in your client's Redirect URI
list. If your application uses a redirect URI that is not white-listed in your PlusAuth Client, you will receive an error.
The same thing applies to the logout URIs. After the user logs out, you need a URI to be redirected.
Configure React Native to use PlusAuth
Create a React Native application or download the sample project from the link on the top of the page.
Install OIDC Client
For interacting with PlusAuth it is advised to use an OpenID Connect library. In this tutorial, we will be using the react-native-app-auth
library. However, you could use any other OpenID Connect library.
Android Setup
Add redirect scheme to your Android app module's build.gradle
file.
iOS Setup
Follow the iOS Setup instructions in React Native AppAuth
docs.
Configure OIDC Client
We need to initialize our React Native AppAuth library to handle authentication-related operations. Create a new config
instance in App.js
.
Replace clientId
value with your Client Id
defined in the Configure Client section. Also change the issuer
value with your Tenant Id
.
Implement login, user profile, and logout
Add the following sections to your App.js
file under the root of your project.
Before implement login
action, create an authState
object to store the retrieved tokens
.
Add Login
We will be using the authorize
method to authenticate the user. The application redirects you to the PlusAuth
login page using the browser.
Add the following section to your App.js
file to start an authentication process.
Get Authenticated User Info
You can get authenticated user info by adding the following section:
Add Logout
Add the following section to log out from the application and PlusAuth:
Clear all the tokens after the successful logout process.
Add View
Finally, add the following section to your App.js
to interact with the user.
See it in action
That's it. Start your app and point to your device or emulator. Follow the Log In link to log in or sign up to your PlusAuth tenant. After you click the Login
button, the browser launches and shows the PlusAuth Login page. Upon successful login or signup, you should be redirected back to the application.