Lesson 49 of 60 – Protected Screens in React Native
82%

Protected Screens in React Native

Protected screens are screens that should only be available to authenticated users. They are commonly used in applications that have login and logout functionality.

For example, a student dashboard should normally be available only after the student has successfully logged in.

Login
  ↓
Authentication
  ↓
Protected Screens
  ├── Dashboard
  ├── Courses
  ├── Assignments
  └── Profile
Note: Protected screen navigation controls the user interface flow. A real application should also enforce authorization on the backend/API because client-side navigation alone is not a security boundary.

1. What is a Protected Screen?

A protected screen is a screen that is intended to be accessed only by authenticated users.

Examples include:

  • Student Dashboard
  • User Profile
  • Orders
  • Account Settings
  • Private Courses
  • Payment History

2. Why Use Protected Screens?

Applications often contain both public and private content. Protected screens help organize the navigation between them.

Public
 ├── Login
 ├── Register
 └── About

Protected
 ├── Dashboard
 ├── Profile
 └── Settings

3. Public and Protected Screens

A public screen can normally be displayed without authentication.

A protected screen requires an authenticated user.

Screen Type Example
Public Login
Public Register
Protected Dashboard
Protected Profile

4. Authentication State

The application needs an authentication state to determine whether the user is logged in.

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

Here, false means the user is not currently authenticated.

5. Changing the Authentication State

After a successful login, the authentication state can be changed.

setIsLoggedIn(true);

When the user logs out:

setIsLoggedIn(false);

The navigation can respond to these state changes.

6. Creating a Protected Stack

A stack navigator can contain screens that are intended for authenticated users.

function AppStack() {

    return (

        <Stack.Navigator>

            <Stack.Screen
                name="Dashboard"
                component={DashboardScreen}
            />

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

        </Stack.Navigator>

    );

}

7. Creating an Authentication Stack

The authentication stack can contain screens such as Login and Register.

function AuthStack() {

    return (

        <Stack.Navigator>

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

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

        </Stack.Navigator>

    );

}

8. Conditional Protected Navigation

The root navigator can choose between the authentication stack and the protected application stack.

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

When isLoggedIn is true, the protected application screens are displayed.

9. Protecting the Dashboard

A dashboard is commonly a protected screen.

function DashboardScreen() {

    return (

        <View>

            <Text>
                Welcome to Dashboard
            </Text>

        </View>

    );

}

The Dashboard should be included in the authenticated navigation tree.

10. Protecting the Profile Screen

function ProfileScreen() {

    return (

        <View>

            <Text>
                My Profile
            </Text>

        </View>

    );

}

The Profile screen can be registered inside the protected stack.

11. Protecting Multiple Screens

A single authenticated navigation tree can contain multiple protected screens.

AppStack
   |
   ├── Dashboard
   ├── Courses
   ├── Assignments
   ├── Profile
   └── Settings

All of these screens can be displayed only when the user is authenticated.

12. Protected Screens with Bottom Tabs

Protected screens can also be organized using bottom tab navigation.

Authenticated User
       ↓
    Tab Navigator
       |
       ├── Home
       ├── Courses
       └── Profile

The entire tab navigator can be part of the authenticated application.

13. Protected Screens with Drawer Navigation

Drawer navigation can also be used after authentication.

Authenticated User
       ↓
    Drawer
       |
       ├── Dashboard
       ├── Courses
       ├── Profile
       └── Settings

The drawer itself can be included inside the authenticated navigation tree.

14. Redirecting Unauthenticated Users

If the user is not authenticated, the application should keep them in the authentication flow instead of displaying protected application screens.

if (!isLoggedIn) {

    return <AuthStack />;

}

return <AppStack />;

This creates a simple protected navigation pattern.

15. Login Before Protected Screens

Start
  ↓
Login Screen
  ↓
Validate User
  ↓
Successful Login
  ↓
Protected Navigation
  ↓
Dashboard

The protected navigation becomes available after successful authentication.

16. Logout from a Protected Screen

A logout button can be placed inside a protected screen.

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

The logout callback can update the authentication state.

setIsLoggedIn(false);

17. Protected Screens and Navigation State

When authentication changes, the root navigation structure can also change.

Logged Out
    ↓
AuthStack

Logged In
    ↓
AppStack

This keeps authentication-related navigation separate from application navigation.

18. Checking Authentication on App Start

When an application starts, it may need to determine whether a previous authentication session is still valid.

App Starts
    ↓
Check Authentication
    ↓
Loading
    ↓
Authenticated?
  /       \
Yes       No
 ↓         ↓
AppStack  AuthStack

19. Loading State for Protected Navigation

Authentication checks can take time. A loading state can be used while the application determines the authentication state.

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

After the authentication check:

setLoading(false);

The application can then display the appropriate navigation.

20. Protected Screens with API Authentication

Many applications use an API to authenticate users.

Login Form
    ↓
API Request
    ↓
Server Verification
    ↓
Authentication Result
    ↓
Update Auth State
    ↓
Protected Navigation

The client should not assume that simply opening a protected screen proves that the user is authorized.

21. Protected Screens and User Roles

Some applications have different types of authenticated users. For example:

User
 ├── Dashboard
 └── Profile

Admin
 ├── Dashboard
 ├── Users
 ├── Reports
 └── Settings

The application can use authentication and authorization information to determine which navigation options should be displayed.

22. Protected Student Screens

An education application may protect student-specific screens.

Student Login
      ↓
Student Dashboard
      |
      ├── Courses
      ├── Assignments
      ├── Results
      ├── Attendance
      └── Profile

These screens can be placed inside authenticated navigation.

23. Protected Screens and Parameters

Protected screens can also receive navigation parameters.

navigation.navigate(
    "CourseDetails",
    {
        courseId: 101
    }
);

The CourseDetails screen can then read the parameter using route.params.

The screen itself can remain inside the authenticated navigation tree.

24. Protected Screens with Nested Navigation

Protected navigation can contain nested navigators.

AuthStack
    |
    └── Login

AppStack
    |
    └── MainDrawer
          |
          └── MainTabs
                |
                ├── Home
                ├── Courses
                └── Profile

This is useful for larger applications with multiple navigation levels.

25. Avoiding Direct Access to Protected UI

The navigation structure should not intentionally expose private screens to unauthenticated users.

Unauthenticated
      ↓
Authentication Screens

Authenticated
      ↓
Private Application Screens

However, hiding a screen in the mobile UI is not enough to secure private data. Backend services should also verify authentication and permissions.

26. Common Protected Screen Mistakes

  • Displaying protected screens before authentication is checked.
  • Keeping authentication state in multiple unrelated places.
  • Not handling the initial loading state.
  • Not handling logout correctly.
  • Allowing protected API requests without server-side verification.
  • Mixing authentication and application navigation unnecessarily.
  • Ignoring failed authentication requests.
  • Forgetting to handle expired sessions.

27. Complete Protected Screen 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 DashboardScreen({
    onLogout
}) {

    return (

        <View>

            <Text>
                Protected Dashboard
            </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="Dashboard"
            >

                {() => (
                    <DashboardScreen
                        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. Protected Screen Best Practices

  • Keep public and protected navigation clearly separated.
  • Maintain a clear authentication state.
  • Show a loading state while checking authentication.
  • Handle login failures properly.
  • Provide a clear logout action.
  • Use appropriate authorization checks for different user roles.
  • Protect sensitive data on the backend as well.
  • Keep nested navigation understandable.

29. Important Protected Screen Concepts

Concept Purpose
Protected Screen Screen intended for authenticated users
Auth Stack Contains public authentication screens
App Stack Contains protected application screens
Authentication State Represents whether the user is authenticated
Loading State Used while authentication is being checked
Logout Ends the authenticated application flow
Authorization Determines what an authenticated user is allowed to access

30. Complete Protected Screen Workflow

Start Application
        ↓
Check Authentication
        ↓
     Loading
        ↓
Is User Authenticated?
       /       \
     No         Yes
     ↓           ↓
 Auth Stack   App Stack
     ↓           ↓
   Login     Dashboard
                 ↓
          Protected Screens
          ├── Courses
          ├── Profile
          ├── Settings
          └── Assignments
                 ↓
               Logout
                 ↓
             Auth Stack

Protected screens are an important part of mobile application navigation. They help separate public authentication screens from screens intended for authenticated users. In production applications, navigation controls should be combined with server-side authentication and authorization for actual data protection.

📌 Key Points

  • Protected screens are intended for authenticated users.
  • Public screens can be displayed without authentication.
  • An authentication state can control which navigation tree is shown.
  • Auth Stack can contain Login and Register screens.
  • App Stack can contain protected application screens.
  • Protected screens can be combined with Stack, Tab and Drawer navigation.
  • Loading states are useful while checking authentication.
  • Logout should return the application to the authentication flow.
  • Different user roles may require different protected screens.
  • Backend authorization is also required to protect sensitive data and operations.

🧠 Quick Quiz

Question: What is a protected screen?