Forgot Password Logic - abhiram-shaji/Langroove GitHub Wiki
This documentation outlines the functionality of the useForgotPassword
hook, focusing on the implementation of the password reset feature in a Firebase-authenticated application.
The useForgotPassword
hook manages the password reset flow by:
- Capturing the user's email input.
- Sending a password reset email using Firebase's authentication service.
- Providing navigation to the Login screen after a successful reset request or on demand.
- To store and manage the user's email input for requesting a password reset.
- Utilizes React's
useState
hook to capture and update theemail
field dynamically.
const [email, setEmail] = useState<string>('');
- The
email
state variable holds the email entered by the user. - The
setEmail
function updates the state as the user types.
- To send a password reset email to the user via Firebase Authentication.
-
Functionality:
- Calls Firebase's
sendPasswordResetEmail
method with the provided email address. - Logs a success message on successful email delivery.
- Navigates to the Login screen post-success.
- Calls Firebase's
-
Code Snippet:
const handleResetPassword = () => { if (email.trim()) { sendPasswordResetEmail(auth, email) .then(() => { console.log(`Password reset link sent to ${email}`); navigation.navigate('Login'); // Navigate to Login screen }) .catch((error) => { console.error('Error sending password reset email:', error.message); alert('Error sending password reset email. Please try again.'); }); } else { alert('Please enter a valid email address.'); } };
-
Process:
- Validates that the email input is non-empty and trimmed of whitespace.
- On success, navigates to the
Login
screen. - Provides error feedback for invalid inputs or failed attempts.
- To redirect the user to the Login screen when required.
-
Functionality:
- Utilizes the
useNavigation
hook to navigate programmatically.
- Utilizes the
-
Code Snippet:
const navigateLogin = () => { navigation.navigate('Login'); // Navigate to Login screen };
-
Process:
- Calls the
navigate
method to switch to theLogin
screen.
- Calls the
Property/Method | Description |
---|---|
email |
The user's email input for the password reset request. |
setEmail |
Updates the email state variable based on user input. |
handleResetPassword |
Sends a password reset email and navigates to the Login screen on success. |
navigateLogin |
Navigates directly to the Login screen. |
- Alerts provide feedback for:
- Missing or invalid email inputs.
- Errors during the password reset request (e.g., invalid email format, network issues).
- Firebase Authentication caches the user's session state but does not cache email addresses for password reset requests. The
email
state is managed locally within the hook.
- Use this hook in screens that provide a "Forgot Password" feature, linking it to a form input for the user's email.
For additional reference, consult the Firebase Authentication Documentation.