Mobile application engineers will consistently need to implement authentication in their day to day app development. This could be because of many reasons like - limiting a region from unauthenticated clients, holding a few assets for just authenticated clients and so forth.
This article I will walk you through how to implement a straightforward yet a powerful and real-world authentication flow in react native application.
My main focus will be on the explanation of the process and will not talk about building and styling the all the screen layout. The complete source code is however available on my github repository.
Prerequisites
To follow along with this tutorial you need the following:
- Node version ≥10.x.x installed
- A package manager such as npm or Yarn
- react-native-cli or expo-cli installed on your local machine
I will be using React Native version 0.64.3 and React Navigation version 6.x. For complete environment setup for react native development checkout the official documentation.
The Auth Flow
The authentication flow of the app will typically look like this:
- The user launch the app.
- The app tries to loads some authentication data from persistent storage (in our case AsyncStorage).
- If auth data is found in the storage, this will be passed in to the auth state and the user will be presented with the main app.
- If auth data is not found the user is presented with the authentication screen and will only be able to navigate through all the authentication screens only.
- When the user signs out, all authentication data will be removed from the storage and auth state will be updated and user is sent back to authentication screens.
Creating React Native Project
The Approach
We want to prevent user from gaining access to all the main app screens by clicking on the back button after they have logged out. The best way to prevent this is to unmount all of the screens related to main app screens when they logout. To achieve this, we will be using React Navigation Library.
Adding Screens
Since we will be having some different set of screens for authenticated and unauthenticated users, let's create four screens within our src/screens folder- LoginScreen and RegisterScreen for unauthenticated users and DashboardScreen and AccountScreen for authenticated users.
Adding Navigation
Let's add React Navigation library. For a comprehensive documentation on adding navigation to your react native project can be found in React Navigation Library Docs
React Navigation is made up of some core utilities and those are then used by navigators to create the navigation structure in your app. We will install some couple of libraries.
React Navigation Library Installations - React Native CLI
React Navigation Library Installations - Expo Native CLI
Installing the native stack navigator library
Creating the app native stack navigators
And navigator.js
Now we are done with the app's navigation. Implementing redux is next.
Adding Redux Toolkit and React-Redux
This creates a Redux store, but it's expecting authSlice, let's create that next.
Create a Redux State Slice
Add a new file named src/redux/slices/authSlice.js. In that file, import the createSlice API from Redux Toolkit. Creating a slice requires a string name to identify the slice, an initial state value, and one or more reducer functions to define how the state can be updated. Once a slice is created, we can export the generated Redux action creators and the reducer function for the whole slice.
Implement State Change in the Navigation.js
import { useSelector } from 'react-redux';
import { selectIsLoggedIn } from '../redux/slices/authSlice';
The complete code should look like this:
Implement Login Logic
In the LoginScreen.js, let's make a new function named handleLogin. In the function, we will dispatch setSignIn action of our redux slice and pass in the state object to update the auth variables
Post a Comment
0Comments