/profile
screen that includes cross-device session synchronization and automatic navigation. In addition, we will see how to show conditional navigation and content based on authentication status.
This is Part 2 in the Full-Stack React Native Development with Nhost series. This part builds a foundation for authentication-protected screens that you can extend to secure any part of your application.
Full-Stack React Native Development with Nhost
1. Create Project
Set up your Nhost project
2. Protected Screens
Current - Screen protection basics
3. User Authentication
Complete auth flow
4. GraphQL Operations
CRUD operations with GraphQL
5. File Uploads
File upload and management
6. Sign in with Apple
Apple authentication integration
Prerequisites
- An Nhost project set up
- Node.js 20+ installed
- Expo CLI installed globally (
npm install -g @expo/cli
) - Basic knowledge of React Native and Expo Router
Step-by-Step Guide
1
Create a New React Native App
We’ll start by creating a fresh React Native application using Expo with TypeScript support. Expo provides fast development tools and optimized builds for modern React Native applications.2
Install Required Dependencies
Install the Nhost JavaScript SDK and AsyncStorage for client-side routing and storage. The Nhost SDK handles authentication, while AsyncStorage enables persistent session storage.3
Configure Expo Router Entry Point
Update yourpackage.json
to use Expo Router as the main entry point by adding or replacing the main
field (leave rest of the file unchanged):package.json
4
Environment Configuration
Let’s configure our app to connect to your Nhost backend. We’ll use Expo’sapp.json
to store environment variables.app.json
Replace
<region>
and <subdomain>
with the actual values from your Nhost project dashboard.5
Create the AsyncStorage Adapter
Build a storage adapter that enables Nhost to persist sessions using React Native’s AsyncStorage. This provides session persistence across app restarts and updates.app/lib/nhost/AsyncStorage.tsx
6
Create the Nhost Auth Provider
Build the core authentication provider that manages user sessions across your application. This component provides authentication state to all child components and handles cross-device synchronization.app/lib/nhost/AuthProvider.tsx
7
Create Shared Theme and Styles
Before building components, let’s create shared theme constants and common styles to keep our code clean and maintainable. This follows React Native best practices for styling.Create the theme constants file:
app/styles/theme.ts
8
Create the Protected Screen Component
Build a reusable component that wraps protected screens and handles authentication checks. This component prevents unauthorized access and provides loading states during authentication verification.app/components/ProtectedScreen.tsx
9
Create the Profile Screen
Create a screen that displays user information. Note that the screen itself doesn’t need any special authentication logic - the screen protection is handled entirely by wrapping it with the ProtectedScreen component from the previous step.app/profile.tsx
10
Update the Home Screen
Build a public homepage that adapts its content based on authentication status. This shows users different options depending on whether they’re signed in.app/index.tsx
11
Update the Main App Layout
Configure the application’s routing structure with Expo Router. This sets up the route hierarchy, includes the authentication context to the entire app, and configures the navigation.app/_layout.tsx
12
Run and test the Application
Start the development server to test your screen protection implementation:- Open the app in Expo Go on your device or simulator.
- Because you are not signed in, the home screen should show a message indicating that you are not signed in.
- Additionally the profle button should not be visible.
How It Works
- AuthProvider: Manages authentication state using Nhost’s client with AsyncStorage and provides it through React Context
- ProtectedScreen: A wrapper component that checks authentication status before rendering child screens
- Profile Screen: A protected screen that displays user information, only accessible when authenticated
Key Features Demonstrated
Screen Protection
Screen Protection
Screens are protected using Expo Router and authentication context, preventing unauthorized access to sensitive areas.
Loading States
Loading States
Smooth loading indicators are shown during authentication checks to improve user experience.
Automatic Navigation
Automatic Navigation
Cross-device Synchronization
Cross-device Synchronization
Authentication state is synchronized across multiple devices using Nhost’s session storage events with AsyncStorage.
Session Management
Session Management
Complete user session and profile information is displayed and managed throughout the application.