Skip to content
Advertisement

How to considered soft delete in laravel inbuilt reset password

I want to apply soft delete in laravel5.4 inbuilt reset password. Due to duplicate email of soft delete deleted email password is change but not the correct one. I am getting stuck to where apply deleted should be null instead of email checking only. that’s why it fetches the deleted record insted of correct one. My reset password controller is given below. Please check my reset controller & suggest any solution please.

class ResetPasswordController extends Controller
{
    use ResetsPasswords;

    protected $redirectTo = 'member/welcome';

    public function showResetForm(Request $request, $token = null)
    {
        return view('frontend.member.auth.passwords.reset')->with(
            ['token' => $token, 'email' => $request->email]
        );
    }
    
    public function reset(Request $request)
    {
        $this->validate($request, $this->rules(), $this->validationErrorMessages());
        $response = $this->broker()->reset(
            $this->credentials($request), function ($user, $password) {
                $this->resetPassword($user, $password);
            }
        );

        return $response == Password::PASSWORD_RESET
                    ? $this->sendResetResponse($response)
                    : $this->sendResetFailedResponse($request, $response);
    }

    protected function rules()
    {
        return [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:8|regex:/^.*(?=.{3,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[dx])(?=.*[@!$#%^*()-_]).*$/',
        ];
    }
    /**
     * Get the password reset validation error messages.
     *
     * @return array
     */
    protected function validationErrorMessages()
    {
        return [
            'password.regex' => 'The password must contain at least one uppercase, one lowercase, one number and one special(@!$#%...) character.'
                ];
    }

    protected function resetPassword($user, $password)
    {
        $password = app('hash')->needsRehash($password) ? Hash::make($password) : $password;

        $user->forceFill([
            'password' => $password,
            'remember_token' => Str::random(60),
        ])->save();

        $this->guard()->login($user);
    }

    
    public function broker()
    {
        return Password::broker('members');
    }

    protected function guard()
    {
        return Auth::guard('web_member');
    }
}

Please help thanks in advance.I am new to laravel please help.

Advertisement

Answer

Just need to pass the correct user to resetPassword() function like

public function reset(Request $request)
{
    $this->validate($request, $this->rules(), $this->validationErrorMessages());
    $response = $this->broker()->reset(
        $this->credentials($request), function ($user, $password) {
            $user = User::where('email', $user->email)->whereNull('deleted_at')->first();
            $this->resetPassword($user, $password);
        }
    );

    return $response == Password::PASSWORD_RESET
                ? $this->sendResetResponse($response)
                : $this->sendResetFailedResponse($request, $response);
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement