ForgotPasswordController.php

This handles the verification of a user's email, sending a one-time password (OTP) for password reset, and resetting the user's password when provided with the correct user ID and new password.

verifyEmail

public function verifyEmail(Request $request)
{
    // Get the email address from the request
    $email = $request->input('email');
    // Check if a user account with the provided email exists
    $userAccount = UserAccount::where('email', $email)->first();
    if (isset($userAccount)) {
        // Generate a random OTP (One-Time Password)
        $otp = strval(mt_rand(100000, 999999));
        // Compose the email subject and body with the OTP
        $subject = "Reset password";
        $body = "<div style='display:block'>OTP:&nbsp;" . $otp . "</div>";
        // Send the email with the OTP to the user's email address
        Mail::to($userAccount->email)->send(new EmailContent($subject, $body));
        // Prepare the success response with OTP and user details
        $response = [
            'SUCCESS' => 1,
            'OTP' => $otp,
            'USER_ID' => $userAccount->id,
        ];
        return $response;
    } else {
        // If the user account is not found, return a failure response
        $response = [
            'SUCCESS' => 0,
        ];
        return $response;
    }
}

To explain what this verifyEmail method does:

  1. Retrieve Email: Obtain the email address from the incoming request.

  2. Check User Existence: Verify if a user account associated with the provided email exists in the database.

  3. Generate OTP: Create a random six-digit One-Time Password (OTP).

  4. Compose Email Content: Build the email subject as "Reset password" and the body containing the generated OTP.

  5. Send Email: Dispatch the email with the OTP to the user's email address using Laravel's Mail service.

  6. Prepare Success Response:

    • Set 'SUCCESS' to 1 to indicate a successful operation.

    • Include the generated OTP in the response.

    • Add the user's ID to the response.

  7. Return Response: Return the response containing the success status, OTP, and user ID. If the user account is not found, return a failure response with 'SUCCESS' set to 0.

resetPassword

public function resetPassword(Request $request)
{
    // Get user ID and new password from the request
    $userId = $request->input('userId');
    $newPassword = $request->input('password');
    // Find the user account by ID
    $userAccount = UserAccount::where('id', $userId)->first();
    if (isset($userAccount)) {
        // If the user account is found, update the password
        $userAccount->password = $newPassword;
        $userAccount->save();
        // Prepare the success response
        $response = [
            'SUCCESS' => 1,
        ];
        return $response;
    } else {
        // If the user account is not found, return a failure response
        $response = [
            'SUCCESS' => 0,
        ];
        return $response;
    }
}

To explain what this resetPassword method does:

  1. Retrieve Data: Get the user ID and the new password from the incoming request.

  2. Find User Account: Search for the user account in the database using the obtained user ID.

  3. Check User Existence: Verify if the user account associated with the provided ID exists.

  4. Update Password: If the user account is found, update its password with the new password provided in the request.

  5. Save Changes: Save the updated user account information in the database.

  6. Prepare Success Response:

    • Set 'SUCCESS' to 1 to indicate a successful password reset.

  7. Return Response: Return the response indicating the success status. If the user account is not found, return a failure response with 'SUCCESS' set to 0.

Last updated