Forgot Password Logic - abhiram-shaji/Langroove GitHub Wiki

Developer Documentation: useForgotPassword.ts Hook

This documentation outlines the functionality of the useForgotPassword hook, focusing on the implementation of the password reset feature in a Firebase-authenticated application.


Overview

The useForgotPassword hook manages the password reset flow by:

  1. Capturing the user's email input.
  2. Sending a password reset email using Firebase's authentication service.
  3. Providing navigation to the Login screen after a successful reset request or on demand.

Features and Caching

Email State Management

Purpose

  • To store and manage the user's email input for requesting a password reset.

Implementation

  • Utilizes React's useState hook to capture and update the email field dynamically.

Code Snippet

const [email, setEmail] = useState<string>('');

Process

  • The email state variable holds the email entered by the user.
  • The setEmail function updates the state as the user types.

Password Reset Email

Purpose

  • To send a password reset email to the user via Firebase Authentication.

Implementation

  1. 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.
  2. 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.');
      }
    };
  3. 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.

Login Screen Navigation

Purpose

  • To redirect the user to the Login screen when required.

Implementation

  1. Functionality:

    • Utilizes the useNavigation hook to navigate programmatically.
  2. Code Snippet:

    const navigateLogin = () => {
      navigation.navigate('Login'); // Navigate to Login screen
    };
  3. Process:

    • Calls the navigate method to switch to the Login screen.

Key Properties and Methods

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.

Error Handling

  • Alerts provide feedback for:
    • Missing or invalid email inputs.
    • Errors during the password reset request (e.g., invalid email format, network issues).

Notes

Caching and State

  • 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.

Usage Scenarios

  • 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.

⚠️ **GitHub.com Fallback** ⚠️