Lesson 48 of 60 – Authentication Navigation in React Native
80%

Authentication Navigation in React Native

Authentication Navigation is used to control which screens a user can access depending on whether the user is logged in or logged out.

For example, a mobile application may show a Login screen before authentication and the main application screens after successful login.

Application
     |
     ├── Not Logged In
     │      ├── Login
     │      └── Register
     │
     └── Logged In
            ├── Home
            ├── Courses
            ├── Profile
            └── Settings
Note: Authentication navigation controls the navigation flow. It does not replace the actual authentication system used to verify a user's identity.

1. What is Authentication Navigation?

Authentication Navigation means changing the available navigation screens according to the user's authentication state.

A common flow is:

Login
  ↓
Authentication
  ↓
Main Application

When the user logs out, the application can return to the Login screen.

2. Why is Authentication Navigation Important?

Most applications contain screens that should only be available to authenticated users.

  • Home screen
  • Profile screen
  • Dashboard
  • Courses
  • Orders
  • Settings

Authentication navigation helps organize access to these screens.

3. Logged-In and Logged-Out States

An application normally needs to know whether the user is currently authenticated.

isLoggedIn = false

When the user successfully logs in:

isLoggedIn = true

The navigation interface can then change according to this value.

4. Creating Authentication State

React's useState hook can be used to store a simple authentication state.

import React, {
    useState
} from 'react';

const [isLoggedIn, setIsLoggedIn] =
    useState(false);

Initially, the user is considered logged out.

5. Showing the Login Screen

When the user is not authenticated, the application can display the Login screen.

if (!isLoggedIn) {
    return <LoginScreen />;
}

The user can then enter their credentials and attempt to log in.

6. Showing the Main Application

When authentication is successful, the application can display the main navigation.

if (isLoggedIn) {
    return <MainApp />;
}

This creates two basic navigation states.

Logged Out → Login
Logged In  → Main App

7. Authentication Flow

Start Application
        ↓
Check Authentication
        ↓
   Is User Logged In?
      /        \
    No          Yes
    ↓            ↓
 Login       Main App
    ↓            ↓
Success       Home
    ↓
 Main App

This is one of the most common authentication navigation patterns.

8. Creating a Login Screen

function LoginScreen({
    onLogin
}) {

    return (

        <View>

            <Text>
                Login
            </Text>

            <Button
                title="Login"
                onPress={onLogin}
            />

        </View>

    );

}

The onLogin function can be called when the user successfully completes authentication.

9. Updating Authentication State

After a successful login, update the authentication state.

setIsLoggedIn(true);

React will re-render the component and display the authenticated navigation.

10. Creating a Logout Function

A logout function can change the authentication state back to false.

function logout() {

    setIsLoggedIn(false);

}

After logout, the application can display the Login screen again.

11. Login and Main Navigation

A common structure is to keep authentication navigation separate from the main application navigation.

Root Navigation
      |
      ├── Auth Stack
      │     ├── Login
      │     └── Register
      │
      └── App Stack
            ├── Home
            ├── Courses
            └── Profile

12. Creating an Auth Stack

function AuthStack() {

    return (

        <Stack.Navigator>

            <Stack.Screen
                name="Login"
                component={LoginScreen}
            />

            <Stack.Screen
                name="Register"
                component={RegisterScreen}
            />

        </Stack.Navigator>

    );

}

The Auth Stack can contain screens related to authentication.

13. Creating the App Stack

function AppStack() {

    return (

        <Stack.Navigator>

            <Stack.Screen
                name="Home"
                component={HomeScreen}
            />

            <Stack.Screen
                name="Profile"
                component={ProfileScreen}
            />

        </Stack.Navigator>

    );

}

The App Stack contains screens available after authentication.

14. Conditional Navigation

The root component can decide which navigation stack should be displayed.

{isLoggedIn
    ? <AppStack />
    : <AuthStack />
}

This creates a clear separation between authenticated and unauthenticated navigation.

15. Creating the Root Navigation

function RootNavigator() {

    const [
        isLoggedIn,
        setIsLoggedIn
    ] = useState(false);

    return (

        <NavigationContainer>

            {isLoggedIn
                ? <AppStack />
                : <AuthStack />
            }

        </NavigationContainer>

    );

}

The root navigator decides which navigation tree should be visible.

16. Passing the Login Function

The login state is usually controlled by a parent component. The login function can be passed to the Login screen as a prop.

<LoginScreen
    onLogin={() => setIsLoggedIn(true)}
/>

After a successful login, the parent updates the authentication state.

17. Login with TextInput

A real login screen commonly uses TextInput components for email, username or password.

<TextInput
    placeholder="Email"
/>

<TextInput
    placeholder="Password"
    secureTextEntry={true}
/>

The entered values can be stored using useState.

18. Simple Login Handler

function handleLogin() {

    // Authentication logic

    onLogin();

}

In a real application, authentication logic can communicate with a backend API before calling onLogin().

19. Authentication with an API

Many mobile applications send login information to a server.

Login Form
    ↓
API Request
    ↓
Server Verification
    ↓
Success / Failure
    ↓
Update Authentication State

The navigation should change only after the application has determined that authentication was successful.

20. Loading During Login

Login requests may take some time. A loading state can prevent multiple login requests.

const [
    loading,
    setLoading
] = useState(false);

When the login request starts:

setLoading(true);

After the request finishes:

setLoading(false);

21. Protecting Application Screens

Instead of allowing unauthenticated users to access protected screens, the application can display the authentication stack until login is complete.

if (!isLoggedIn) {

    return <AuthStack />;

}

return <AppStack />;

This keeps the navigation structure clear.

22. Logout from the Profile Screen

A logout button can be placed inside the Profile or Settings screen.

<Button
    title="Logout"
    onPress={logout}
/>

The logout function should update the authentication state.

setIsLoggedIn(false);

23. Remembering Login State

If an application needs to remember the user's login state after the application is restarted, authentication information can be stored using an appropriate persistent storage solution.

The application can then load that information when it starts and decide which navigation tree to display.

Application Starts
       ↓
Read Stored Auth State
       ↓
Check Authentication
       ↓
Show Auth or App Navigation

24. Authentication with Drawer and Tabs

After authentication, the main application can contain a more advanced nested navigation structure.

Auth Stack
    |
    └── Login

App Navigation
    |
    └── Drawer
          |
          └── Main Tabs
                |
                ├── Home
                ├── Courses
                └── Profile

This structure is useful for larger applications.

25. Authentication Flow for an Education App

Login
  ↓
Student Verification
  ↓
Student Dashboard
  ↓
Main Navigation
  ├── Home
  ├── Courses
  ├── Assignments
  └── Profile
  ↓
Logout
  ↓
Login

An education application can use this pattern to separate login screens from student application screens.

26. Common Authentication Navigation Mistakes

  • Showing protected screens before authentication is complete.
  • Changing navigation before the login request succeeds.
  • Not handling login failures.
  • Allowing repeated login requests.
  • Keeping authentication state only in unrelated components.
  • Not handling logout correctly.
  • Creating unnecessarily complicated navigation trees.
  • Forgetting to handle the initial authentication state.

27. Complete Authentication Navigation Example

import React, {
    useState
} from 'react';

import {
    View,
    Text,
    Button
} from 'react-native';

import {
    NavigationContainer
} from '@react-navigation/native';

import {
    createNativeStackNavigator
} from '@react-navigation/native-stack';

const Stack =
    createNativeStackNavigator();

function LoginScreen({
    onLogin
}) {

    return (

        <View>

            <Text>
                Login Screen
            </Text>

            <Button
                title="Login"
                onPress={onLogin}
            />

        </View>

    );

}

function HomeScreen({
    onLogout
}) {

    return (

        <View>

            <Text>
                Home Screen
            </Text>

            <Button
                title="Logout"
                onPress={onLogout}
            />

        </View>

    );

}

function AuthStack({
    onLogin
}) {

    return (

        <Stack.Navigator>

            <Stack.Screen
                name="Login"
                options={{
                    headerShown: false
                }}
            >

                {() => (
                    <LoginScreen
                        onLogin={onLogin}
                    />
                )}

            </Stack.Screen>

        </Stack.Navigator>

    );

}

function AppStack({
    onLogout
}) {

    return (

        <Stack.Navigator>

            <Stack.Screen
                name="Home"
                options={{
                    title: "Dashboard"
                }}
            >

                {() => (
                    <HomeScreen
                        onLogout={onLogout}
                    />
                )}

            </Stack.Screen>

        </Stack.Navigator>

    );

}

export default function App() {

    const [
        isLoggedIn,
        setIsLoggedIn
    ] = useState(false);

    return (

        <NavigationContainer>

            {isLoggedIn ? (

                <AppStack
                    onLogout={() =>
                        setIsLoggedIn(false)
                    }
                />

            ) : (

                <AuthStack
                    onLogin={() =>
                        setIsLoggedIn(true)
                    }
                />

            )}

        </NavigationContainer>

    );

}

28. Authentication Navigation Best Practices

  • Keep authentication and application navigation clearly separated.
  • Use a single source of truth for authentication state.
  • Show loading feedback while checking authentication.
  • Handle failed login attempts properly.
  • Provide a clear logout mechanism.
  • Do not expose protected screens before authentication is confirmed.
  • Keep nested navigation understandable.
  • Use persistent storage when the application needs to remember login state.

29. Important Authentication Navigation Concepts

Concept Purpose
Authentication State Stores whether the user is authenticated
Auth Stack Contains login and registration screens
App Stack Contains authenticated application screens
Login Starts the authentication process
Logout Ends the authenticated session
Loading State Indicates that authentication is being checked or processed
Root Navigator Chooses the appropriate navigation tree
Protected Screen A screen intended for authenticated users

30. Complete Authentication Navigation Workflow

Start Application
        ↓
Check Authentication State
        ↓
Show Loading if Necessary
        ↓
Is User Authenticated?
       / \
     No   Yes
     ↓     ↓
 Auth     App
 Stack   Stack
  ↓        ↓
Login    Home
  ↓        ↓
Success  Main App
  ↓        ↓
 App     Logout
 Stack     ↓
  ↓       Auth
 Main App  Stack

Authentication Navigation is essential for applications that have both public and private screens. The application should clearly separate authentication screens from authenticated application screens and update navigation when the authentication state changes.

📌 Key Points

  • Authentication Navigation controls screens based on authentication state.
  • Logged-out users can be shown an Auth Stack.
  • Logged-in users can be shown an App Stack.
  • useState can be used for a basic authentication state.
  • Login should update authentication state after successful authentication.
  • Logout should change the authentication state back to logged out.
  • Protected application screens should not be displayed before authentication.
  • Loading states are useful while authentication is being checked.
  • Authentication navigation can be combined with Drawer, Tab and Stack navigation.
  • A clear authentication flow makes the navigation structure easier to maintain.

🧠 Quick Quiz

Question: What is the main purpose of Authentication Navigation?