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
verifyEmailpublic 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: " . $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:
Retrieve Email: Obtain the email address from the incoming request.
Check User Existence: Verify if a user account associated with the provided email exists in the database.
Generate OTP: Create a random six-digit One-Time Password (OTP).
Compose Email Content: Build the email subject as "Reset password" and the body containing the generated OTP.
Send Email: Dispatch the email with the OTP to the user's email address using Laravel's
Mailservice.Prepare Success Response:
Set
'SUCCESS'to1to indicate a successful operation.Include the generated OTP in the response.
Add the user's ID to the response.
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 to0.
resetPassword
resetPasswordpublic 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:
Retrieve Data: Get the user ID and the new password from the incoming request.
Find User Account: Search for the user account in the database using the obtained user ID.
Check User Existence: Verify if the user account associated with the provided ID exists.
Update Password: If the user account is found, update its password with the new password provided in the request.
Save Changes: Save the updated user account information in the database.
Prepare Success Response:
Set
'SUCCESS'to1to indicate a successful password reset.
Return Response: Return the response indicating the success status. If the user account is not found, return a failure response with
'SUCCESS'set to0.
Last updated