Search code examples
laravelauthenticationlaravel-8laravel-livewirelivewires

Livewire Password Reset Component


The Laravel website states that you can use the following code to reset the users password

$request->validate(['email' => 'required|email']);

$status = Password::sendResetLink(
    $request->only('email')
);

return $status === Password::RESET_LINK_SENT
            ? back()->with(['status' => __($status)])
            : back()->withErrors(['email' => __($status)]);

I wanting to create the password reset function as a Livewire component and have used the following code

$this->validate();

    $status = Password::sendResetLink(
        $this->email
    );

    return $status === Password::RESET_LINK_SENT
            ? back()->with(['status' => __($status)])
            : back()->withErrors(['email' => __($status)]);

I keep getting the error message

Illuminate\Auth\Passwords\PasswordBroker::sendResetLink(): Argument #1 ($credentials) must be of type array, string given

I understand the message is asking for an array, but not sure how to fix it... Any help appreciated


Solution

  • The sendResetLink function expects an array where you're providing a string. The difference between the reference code and your code is the parameter type.

    $status = Password::sendResetLink(
        $request->only('email')
    );
    

    $request->only('email') returns an associative array, that will look like; ['email' => 'you@domain.com']. This is provided to the sendResetLink function.

    Compare that with your implementation.

    $status = Password::sendResetLink(
        $this->email
    );
    

    With your implementation you're only providingyou@domain.com. What you want is to create an array that mimics the one returned by $request->only('email'), so;

    $status = Password::sendResetLink(
        ['email' => $this->email]
    );