CHAPTER 21
Beginner
Authentication in React Native
Updated: May 16, 2026
7 min read
# CHAPTER 21
Authentication in React Native
1. Introduction
Identity is the foundation of almost every modern application. If a user likes a post, adds an item to a cart, or sends a message, the database needs to know *who* took that action. Building a secure authentication system requires orchestrating multiple systems: fetching a token from an API, saving it to the hard drive, and instantly redirecting the user to the Home screen without them pressing a "Back" button. In this chapter, we will master Authentication in React Native. We will combine Axios, Context API, AsyncStorage, and React Navigation to build a professional, un-hackable "Protected Routes" login flow.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the mechanics of JSON Web Tokens (JWT).
- Architect a global AuthContext.
- Build a Login screen that fetches and saves tokens.
- Conditionally render navigation stacks (Protected Routes).
- Implement an automatic session-restore feature on app launch.
3. The JWT Mechanics
When a user types their email and password and hits "Login", an Axios POST request is sent to the server. The server verifies the password and replies with a JSON Web Token (JWT). This token is a cryptographically signed string that proves the user is who they say they are. The Goal: The mobile app must catch this token, save it toAsyncStorage, and attach it to the Headers of all future Axios requests.
4. Step 1: The Auth Context
We need a global cloud to hold the token so the entire app knows if the user is logged in.
javascript
5. Step 2: Protected Routes (The Magic Switch)
InApp.js, we wrap our app in the AuthProvider.
Then, we use a standard if/else statement inside our <NavigationContainer>.
-
If
userTokenisnull, we return the<AuthStack>(Login, Signup).
-
If
userTokenexists, we return the<MainStack>(Home, Profile).
javascript
6. Step 3: The Login Screen
The user types their credentials. We hit the API, get the token, and feed it to the Context.
javascript
7. Visual Learning: The Authentication Flow
txt
8. Common Mistakes
-
Navigating to Home Manually: Beginners often write
navigation.navigate('Home')inside thehandleLoginfunction. DO NOT DO THIS. If you navigate manually, the user can press the physical "Back" button on Android and accidentally go back to the Login screen while authenticated! By conditionally rendering the Stacks (Section 5), the Login stack is physically destroyed from memory when the token is received. It is impossible to "go back" to it.
9. Best Practices
-
Token Expiration: JWT tokens expire (usually after 24 hours). If a user opens the app on day 2, the
useEffectwill find the token and send them to theHomeScreen, but the first Axios API call they make will return a401 Unauthorizederror. You must use an Axios Response Interceptor (learned in Ch 19) to catch that 401, trigger thelogout()context function automatically, and kick them back to the Login screen!
10. Practice Exercises
-
1.
In a conditionally rendered navigation architecture, what variable dictates whether the
<AuthStack>or the<MainStack>is actively drawn on the screen?
-
2.
What asynchronous function is triggered inside the
useEffecton app launch to seamlessly log a returning user straight into the dashboard?
11. MCQs with Answers
Question 1
Why is conditionally rendering navigation stacks (e.g., userToken ? <AppStack/> : <AuthStack/>) architecturally superior to simply using navigation.navigate('Home') after a successful login?
Question 2
When the app first launches, a developer displays a <SplashScreen /> for one second. What critical background operation is usually occurring during this specific delay?
12. Interview Questions
- Q: Explain the paradigm of "Protected Routes" in React Navigation. How is the global AuthContext utilized to enforce this boundary?
- Q: Walk through the complete lifecycle of a JWT authentication flow, starting from the user pressing "Submit" on the Login form, the token caching strategy, and ending with the execution of a secure Axios interceptor on subsequent API requests.
- Q: A returning user launches your app, reads their valid JWT from AsyncStorage, and is successfully routed to the Dashboard. 5 minutes later, their token expires on the backend. Describe the exact error handling architecture required to gracefully eject the user back to the Login screen.
13. FAQs
Q: How do I implement "Sign in with Google" or "Sign in with Apple"? A: You install specific Expo packages (likeexpo-auth-session or @react-native-google-signin/google-signin). These packages launch the native OS popup. Upon success, Apple/Google hands your app a secure Token. You take that token, send it to YOUR backend server for validation, and proceed with the exact same Context flow outlined above!
14. Summary
In Chapter 21, we fortified our application architecture. We transitioned from isolated UI screens to a globally integrated, state-driven routing system. We established a global AuthContext to track the user's session, utilized AsyncStorage to securely rehydrate that session across app restarts, and executed secure Axios requests to procure JWTs. Crucially, we abandoned manual.navigate() logic for login flows, adopting the Protected Routes paradigm to conditionally mount and destroy entire navigation stacks based on cryptographic identity.