Member-only story
How to add protected routes (sign up and Login) to react-native using react-navigation 4.0 — part 2
This is part 2 of my “how to add protected routes to react native app” tutorial which will discuss the login and logout code.
We will need to first open our file we created last time named “AuthLoading”, if you remember we created a method called “DetermineScreenNavigation”, we need to make that asynchronous by adding the keyword “async” before the “()” parameters so the method will now look like the below.
DetermineScreenNavigation = async () =>{}
The next thing we need to do is to retrieve an item from asynchronous storage, we will be setting this in the login method in the future. The way to do this is use the “getItem” method of the “AsyncStorage” component like the below. You will need to import the “AsyncStorage” component from “react-native” first though.
const userToken = await AsyncStorage.getItem('userToken');
We then need to add a navigation action which will then use a ternary operator to determine which page to navigate to. So if there is an item in asynchronous storage with the name “userToken” then it knows that a user has already logged in as this token is only created when a user logs in as it is used to validate a user, so it would…