Logout Logic - abhiram-shaji/Langroove GitHub Wiki
useLogout
Hook
Developer Documentation: This documentation provides an explanation of the useLogout
hook, which handles the logout process, cache clearing, and navigation reset in a Firebase-authenticated React Native application.
Overview
The useLogout
hook:
- Stops active Firestore snapshot listeners to free up resources.
- Clears locally stored data using AsyncStorage.
- Signs out the user from Firebase Authentication.
- Resets the navigation stack to the login screen.
Features and Functionality
1. Stopping Snapshot Listeners
Purpose
- To terminate active Firestore snapshot listeners when the user logs out, preventing unnecessary resource usage.
Implementation
-
Functionality:
- Uses a reference (
unsubscribeRef
) from theuseFeed
hook to access and stop active snapshot listeners.
- Uses a reference (
-
Code Snippet:
if (unsubscribeRef?.current) { unsubscribeRef.current(); // Unsubscribe from snapshot listener console.log('Snapshot listener stopped.'); }
-
Process:
- Checks if
unsubscribeRef.current
is defined before invoking it. - Logs the successful termination of the listener.
- Checks if
2. Clearing Cache
Purpose
- Clears all locally stored data using
AsyncStorage
to ensure no user-specific data persists after logout.
Implementation
-
Functionality:
- Calls
AsyncStorage.clear()
to remove all data stored in the local device cache.
- Calls
-
Code Snippet:
const clearCache = async () => { try { await AsyncStorage.clear(); console.log('AsyncStorage cleared.'); } catch (error) { console.error('Error clearing AsyncStorage:', error); Alert.alert('Error', 'Failed to clear cache. Please try again.'); } };
-
Process:
- Logs a success message after clearing the cache.
- Displays an error alert if clearing fails.
3. Signing Out
Purpose
- Logs the user out of Firebase Authentication.
Implementation
-
Functionality:
- Uses Firebase's
signOut
method to sign the user out.
- Uses Firebase's
-
Code Snippet:
await signOut(auth);
-
Process:
- Ensures the user is signed out of the app.
- Proceeds to reset the navigation stack after successful logout.
4. Resetting Navigation
Purpose
- Resets the navigation stack and redirects the user to the
Login
screen after logout.
Implementation
-
Functionality:
- Uses React Navigation's
StackActions.replace
to reset the navigation history and navigate to theLogin
screen.
- Uses React Navigation's
-
Code Snippet:
navigation.dispatch( StackActions.replace('Login') // Clears navigation history );
-
Process:
- Prevents users from navigating back to previous screens after logging out.
Key Properties and Methods
Property/Method | Description |
---|---|
clearCache |
Clears all locally stored data using AsyncStorage. |
handleLogout |
Handles the complete logout process, including stopping listeners, clearing cache, signing out, and navigation reset. |
unsubscribeRef |
Reference to active snapshot listeners managed by the useFeed hook. |
Error Handling
-
Cache Clearing Errors:
- Logs the error and displays an alert to the user if clearing AsyncStorage fails.
-
Logout Errors:
- Displays an alert if Firebase sign-out fails.
Notes
useFeed
Dependency on - The hook relies on the
useFeed
hook for accessing theunsubscribeRef
. Ensure thatuseFeed
properly sets up and manages the snapshot listener.
Navigation Reset
- The
StackActions.replace
method ensures the navigation stack is cleared, preventing the user from navigating back to previous screens after logout.
For additional information, refer to the Firebase Authentication Documentation and React Navigation Documentation.