0
votes

I'm trying to set email verified as true if the password reset is completed. Currently, when a user (email not verified) requests a password reset, it does send an email and the user is able to change password. As we can confirm that, email in fact belongs to that user, we should be able to set email verified to true. Currently, Laravel doesn't seem to know when an unverified email requests a password reset.

My reset function on ResetPasswordController.php is something like this(overridden to reset function of ResetsPasswords.php)

    public function reset(Request $request)
    {
        $request->validate($this->rules(), $this->validationErrorMessages());

        // Here we will attempt to reset the user's password. If it is successful we
        // will update the password on an actual user model and persist it to the
        // database. Otherwise we will parse the error and return the response.
        $response = $this->broker()->reset(
            $this->credentials($request),
            function ($user, $password) {
                $this->resetPassword($user, $password);
            }
        );

        // If the password was successfully reset, we will redirect the user back to
        // the application's home authenticated view. If there is an error we can
        // redirect them back to where they came from with their error message.
        return $response == Password::PASSWORD_RESET
            ? $this->sendResetResponse($request, $response)
            : $this->sendResetFailedResponse($request, $response);
    }

How can I let laravel know that User now has a verified email?

Thank you

1

1 Answers

0
votes

Laravel default "email_verified_at" is indeed a timestamp, so you can handle this in several ways:

in your reset method:

   $response = $this->broker()->reset(
            $this->credentials($request),
            function ($user, $password) {
                $this->resetPassword($user, $password);
                $user->email_verified_at = Carbon\Carbon::now(); //add this line
                $user->save(); //add this line
            }
        );

Now the user has a valid timestamp and you can "cast" it to a boolean like this in your User model:

On User.php model class:

      //Some code
      public bool isVerified(){
           if(isset($this->email_verified_at)){
                 return true;
           }
           else{
                 return false;
           }
      }

Now you can use: $user->isVerified() to check if user has verified its email

Hope it helped!