Navigating with Authentication: Implementing Conditional Rendering in Expo Router
In the world of mobile app development, user authentication is a cornerstone of many applications. We want to ensure that only authorized users can access specific screens or functionalities. Expo Router, the new routing system for Expo, provides a seamless way to implement this crucial feature.
Scenario: Protecting Private Screens with Authentication
Let's imagine we're building a mobile app for a fitness platform. The app offers free access to basic workout routines but requires users to sign up for a premium subscription to access advanced features like personalized workout plans.
The Goal: We need to restrict access to the "Premium" screen, which displays personalized workouts, only to users who have successfully authenticated and have an active premium subscription.
Original Code (Without Authentication):
import { useState, useEffect } from 'react';
import { View, Text, Button } from 'react-native';
export default function PremiumScreen() {
return (
<View>
<Text>Welcome to the Premium Workout Plans!</Text>
{/* Display personalized workout plans here */}
</View>
);
}
Implementing Conditional Rendering with Expo Router
Here's how we can leverage Expo Router to implement conditional rendering and protect the "Premium" screen:
-
Authentication State Management: We'll use the
useAuth
hook provided by a suitable authentication library (e.g., Firebase, Auth0). This hook will return the user's authentication status (isLoggedIn
) and other relevant information. -
Conditional Rendering: Inside the
PremiumScreen
component, we'll introduce a conditional statement to check theisLoggedIn
status and the user's subscription status. If both are true, the premium content will be displayed. Otherwise, the user will be redirected to the "Login" screen.
Updated Code with Authentication:
import { useState, useEffect } from 'react';
import { View, Text, Button, useNavigation } from 'expo-router';
import useAuth from '../hooks/useAuth'; // Assuming you have a useAuth hook
export default function PremiumScreen() {
const navigation = useNavigation();
const { isLoggedIn, hasPremiumSubscription } = useAuth();
useEffect(() => {
if (!isLoggedIn || !hasPremiumSubscription) {
navigation.navigate('Login');
}
}, [isLoggedIn, hasPremiumSubscription, navigation]);
return (
<View>
<Text>Welcome to the Premium Workout Plans!</Text>
{/* Display personalized workout plans here */}
</View>
);
}
Advantages of Expo Router for Authentication
- Simplified Routing: Expo Router manages the navigation flow, making it easier to define routes and redirect users based on authentication status.
- Hooks for Navigation: The
useNavigation
hook provides a clean way to access navigation functionalities within your components. - Component-Based Approach: Authentication logic is encapsulated within the
PremiumScreen
component, ensuring better code organization and modularity.
Additional Tips for Secure Authentication
- Store User Information: After successful authentication, store user data (like name, email, subscription status) securely in your application's storage or database.
- Handle Session Expiry: Implement logic to handle session expiry and gracefully redirect users back to the "Login" screen.
- Implement Access Tokens: Use access tokens to securely authorize API calls from your application.
Conclusion
Expo Router simplifies the process of implementing authentication and conditional rendering in your React Native applications. By leveraging its capabilities, you can build secure and robust mobile experiences while ensuring that users have access only to the functionalities they are entitled to.
Remember: The exact implementation details may vary depending on your chosen authentication library and the specific needs of your application. This article provides a general framework to help you get started.