Lesson 134 of 158 – React Native Logout
85%

React Native Logout

Logout is an important part of authentication in a React Native application. When a user logs out, the application should clear the locally stored authentication information and return the user to the authentication flow.

Note: Removing a JWT from React Native stops the client from using that stored token. JWTs are stateless, so immediate server-side invalidation requires an additional revocation strategy such as a denylist or short-lived access tokens with refresh tokens.

1. What is Logout?

Logout is the process of ending the current authenticated session from the application's point of view.

Logout
   ↓
Remove Token
   ↓
Clear User State
   ↓
Clear Authentication State
   ↓
Show Login Screen

2. Why is Logout Important?

  • Prevents the application from continuing to use a stored token.
  • Clears the current user from application state.
  • Returns the user to the authentication flow.
  • Helps protect private information on shared devices.
  • Provides a clear session-ending action.

3. Logout and JWT

A JWT is normally self-contained and does not require a server-side session for every request.

Login
  ↓
JWT Created
  ↓
React Native Stores JWT
  ↓
Protected Requests
  ↓
Logout
  ↓
React Native Removes JWT

Removing the token locally prevents the client from using that stored token in future requests.

4. Removing the Token from AsyncStorage

import AsyncStorage
    from "@react-native-async-storage/async-storage";

await AsyncStorage.removeItem(
    "authToken"
);

This removes the locally stored JWT.

5. Clearing Authentication State

setToken(null);
setUser(null);

Removing the token from storage is not enough if the token and user are still present in React state.

6. Basic Logout Function

const logout = async () => {

    await AsyncStorage.removeItem(
        "authToken"
    );

    setToken(null);
    setUser(null);
};

7. Logout in Auth Context

interface AuthContextType {
    user: User | null;
    token: string | null;
    isAuthenticated: boolean;
    loading: boolean;
    login: (
        token: string,
        user: User
    ) => Promise<void>;
    logout: () => Promise<void>;
}

The logout function can be exposed through Auth Context so any screen can use it.

8. Using Logout with useAuth

const ProfileScreen = () => {

    const {
        user,
        logout
    } = useAuth();

    return (
        <View>

            <Text>
                Welcome {user?.name}
            </Text>

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

        </View>
    );
};

9. Logout Navigation Flow

Profile Screen
      ↓
Logout Button
      ↓
logout()
      ↓
Remove JWT
      ↓
Clear User
      ↓
isAuthenticated = false
      ↓
Auth Navigator
      ↓
Login Screen

10. Conditional Root Navigation

const RootNavigator = () => {

    const {
        isAuthenticated,
        loading
    } = useAuth();

    if (loading) {

        return <LoadingScreen />;
    }

    if (isAuthenticated) {

        return <AppNavigator />;
    }

    return <AuthNavigator />;
};

Clearing authentication state can automatically switch the application back to the authentication navigator.

11. Adding a Logout Confirmation

Alert.alert(
    "Logout",
    "Are you sure you want to logout?",
    [
        {
            text: "Cancel"
        },
        {
            text: "Logout",
            onPress: logout
        }
    ]
);

Confirmation can prevent accidental logout actions.

12. Importing Alert

import {
    Alert
} from "react-native";

React Native's Alert component can be used to confirm sensitive actions such as logout.

13. Complete Logout Handler

const handleLogout = () => {

    Alert.alert(
        "Logout",
        "Do you want to logout?",
        [
            {
                text: "Cancel",
                style: "cancel"
            },
            {
                text: "Logout",
                onPress: async () => {

                    await logout();
                }
            }
        ]
    );
};

14. Why Clear User State?

Suppose the token is removed but the user state remains:

token = null;
user = {
    name: "Rahul",
    email: "rahul@example.com"
};

The UI could still display private user information. Therefore logout should clear both authentication credentials and user state.

15. Clearing Additional Local Data

Applications may store other session-specific information.

await AsyncStorage.removeItem(
    "authToken"
);

await AsyncStorage.removeItem(
    "currentUser"
);

await AsyncStorage.removeItem(
    "selectedStudent"
);

Only remove keys that belong to the current authentication/session design.

16. Clearing a Stored User Object

await AsyncStorage.removeItem(
    "currentUser"
);

setUser(null);

If the user object is persisted separately, it should also be removed during logout.

17. Axios After Logout

After logout, future Axios requests should not receive the old token from the token storage mechanism.

await AsyncStorage.removeItem(
    "authToken"
);

If an Axios interceptor reads the token from AsyncStorage for every request, it will find no token after logout.

18. Handling a 401 Response

try {

    const response =
        await api.get(
            "/profile.php"
        );

} catch (error) {

    if (
        axios.isAxiosError(error) &&
        error.response?.status === 401
    ) {

        await logout();
    }
}

A 401 response may indicate that the token is invalid, expired, or missing.

19. Response Interceptor for 401

api.interceptors.response.use(

    response => response,

    async error => {

        if (
            error.response?.status === 401
        ) {

            await AsyncStorage.removeItem(
                "authToken"
            );
        }

        return Promise.reject(error);
    }
);

A centralized response interceptor can help handle authentication failures consistently.

20. Client Logout vs Server Revocation

Client Logout Server Revocation
Removes local token Server rejects a revoked token
Changes app authentication state Requires server-side state or token strategy
Simple to implement Requires additional backend design

These are related but different concepts.

21. JWT Logout Limitation

A signed JWT normally remains valid until its expiration time unless the server has a mechanism to reject it before expiration.

JWT
 ↓
Signed
 ↓
Stored by Client
 ↓
Logout
 ↓
Client Deletes Token

If immediate server-side invalidation is required, additional revocation logic is needed.

22. Token Revocation Concept

A server can maintain a denylist of revoked token identifiers such as the JWT jti claim.

JWT
 ↓
jti = ABC123
 ↓
Logout
 ↓
Store ABC123 as revoked
 ↓
Future API Request
 ↓
Check Revocation
 ↓
Reject if revoked

This introduces server-side state and additional database or cache work.

23. Short-Lived Access Tokens

Another common design is to use short-lived access tokens and a separate refresh-token mechanism.

Login
  ↓
Access Token
  ↓
Short Lifetime
  ↓
Refresh Token
  ↓
New Access Token

The exact refresh-token implementation should be designed carefully for the application's security requirements.

24. Logout API Endpoint

With a stateless JWT-only design, a logout API is not automatically necessary just to delete the token from the mobile device.

A backend logout endpoint becomes useful when the server needs to revoke refresh tokens, access tokens, sessions, or other server-side authentication state.

POST /api/logout.php

25. Sending a Logout Request

const logoutFromServer =
    async () => {

    try {

        await api.post(
            "/logout.php"
        );

    } catch (error) {

        console.log(
            "Logout API error"
        );

    } finally {

        await logout();
    }
};

Clearing the local authentication state in finally ensures the user can still be logged out locally even if the network request fails.

26. Complete Auth Context Logout

const logout = async () => {

    try {

        await AsyncStorage.removeItem(
            "authToken"
        );

        await AsyncStorage.removeItem(
            "currentUser"
        );

    } finally {

        setToken(null);
        setUser(null);
    }
};

The authentication state is cleared even if local cleanup encounters an error.

27. Complete Logout Screen Example

const SettingsScreen = () => {

    const { logout } =
        useAuth();

    const handleLogout = () => {

        Alert.alert(
            "Logout",
            "Are you sure?",
            [
                {
                    text: "Cancel",
                    style: "cancel"
                },
                {
                    text: "Logout",
                    onPress: async () => {
                        await logout();
                    }
                }
            ]
        );
    };

    return (
        <View>

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

        </View>
    );
};

28. Complete Logout Flow

User Taps Logout
       ↓
Optional Logout API
       ↓
Remove JWT
       ↓
Remove Session Data
       ↓
setToken(null)
       ↓
setUser(null)
       ↓
isAuthenticated = false
       ↓
Auth Navigator
       ↓
Login Screen

29. Logout Security Practices

  • Remove the stored access token.
  • Clear authentication-related application state.
  • Clear session-specific cached data when appropriate.
  • Handle expired or invalid tokens.
  • Use HTTPS for production API communication.
  • Do not log JWTs or passwords.
  • If using refresh tokens, revoke them on logout when appropriate.
  • Use short-lived access tokens when suitable for the application's security model.
  • Do not assume that deleting a JWT locally immediately invalidates that JWT on the server.

30. Complete React Native Logout Architecture

Settings / Profile
        ↓
Logout Button
        ↓
Optional Logout API
        ↓
Remove Access Token
        ↓
Clear User State
        ↓
Clear Session Data
        ↓
isAuthenticated = false
        ↓
Auth Context Updates
        ↓
Root Navigator Updates
        ↓
Login Screen

For server-side revocation:

Logout
  ↓
Revoke Refresh Token /
Token Identifier
  ↓
Future Protected Request
  ↓
Server Rejects Revoked Credential

A complete logout implementation should clear the local authentication state and, when the application's authentication design requires it, perform server-side revocation of reusable credentials.

📌 Key Points

  • Logout removes the locally stored authentication token.
  • Auth Context should clear both the token and user state.
  • AsyncStorage can remove the stored JWT with removeItem().
  • Logout can automatically return the application to the Auth Navigator.
  • A logout confirmation can prevent accidental logout.
  • HTTP 401 responses can trigger authentication cleanup.
  • A JWT is normally stateless and remains valid until expiration unless additional revocation is implemented.
  • Immediate server-side invalidation requires a revocation strategy.
  • Refresh tokens can be revoked during logout in systems that use them.
  • The backend and mobile application should work together to provide secure authentication.

🧠 Quick Quiz

Question: What should React Native normally do with a locally stored JWT when the user logs out?