Benjamin W
Benjamin W

Reputation: 2848

Laravel reset password check extra active column before send reset link

class LoginController extends Controller
{
    #overwrite trait AuthenticatesUsers->credentials
    protected function credentials(Request $request)
    {   
        return array_merge( $request->only($this->username(), 'password'), ['active' => 1]);
    }
}


class ForgotPasswordController extends Controller
{
    //try to overwrite here
}

\vendor\laravel\framework\src\Illuminate\Auth\Passwords\PasswordBroker.php
class PasswordBroker implements PasswordBrokerContract
{
     if (is_null($user) || $user->active != 1) { <-- add $user->active != 1
        return static::INVALID_USER;
     }

}

I custom Laravel register/login system by added email 'active' column.

In my login controller, I overwrite trait to check active column, but I having trouble on reset password.

What I did now is I add $user->active !=1 into PaswordBroker and it works fine, but

I don't want to touch vendor's files and I wish to overwrite it in my controller.

anyone know how to achieve this?

Upvotes: 1

Views: 2003

Answers (2)

kjdion84
kjdion84

Reputation: 10044

It is done within the sendResetLinkEmail of the class, override like so:

public function sendResetLinkEmail(Request $request)
{
    $this->validate($request, [
        'email' => 'required|email',
    ]);

    $response = $this->broker()->sendResetLink([
        'email' => $request->input('email'),
        'active' => true,
    ]);

    return $response == Password::RESET_LINK_SENT
        ? $this->sendResetLinkResponse($response)
        : $this->sendResetLinkFailedResponse($request, $response);
}

Upvotes: 1

Henry Bui
Henry Bui

Reputation: 703

If you want that, try to re-init a new class extends PasswordBroker, after that try to overrite method (add your condition to ...that method).

class YourClass extends PasswordBroker
{ 
    public function sendResetLink(array $credentials, Closure $callback = null)
    {
        $user = $this->getUser($credentials);

        if (is_null($user) || $user->active != 1) {
            return PasswordBrokerContract::INVALID_USER;
        }

        $token = $this->tokens->create($user);

        $this->emailResetLink($user, $token, $callback);

        return PasswordBrokerContract::RESET_LINK_SENT;
    }
}

I don't know your Laravel version are you using, I'm using 5.2.

Hope this help!

Upvotes: 0

Related Questions