首页 > 解决方案 > Custom login property Laravel 8

问题描述

I have this custom function for atempting to login in Laravel 8

 protected function attemptLogin(Request $request)
    {
        $credentials = $this->credentials($request);
        $credentials['estado']=1;
        return $this->guard()->attempt(
            $credentials, $request->filled('remember')
        );
    }

How I can make to accept the login atempt when $credentials['estado'] also has 2 as value. Don't know how to make it accept multiple values.

I managed to make the custom function accept the value of 1 but dunno how to make it accept multiple $credentials['estado'] values.

标签: laravelauthenticationlaravel-8

解决方案


You don't need to change anything in attemptLogin() method, instead you can customize the crededentials() method in LoginController like this:

// login, if user have like a following described data in array
protected function credentials(Request $request)
{
    $username = $this->username();

    return [
        $username => $request->get($username),
        'password' => $request->get('password'),
        'estado' => [ 1, 2 ], // OR condition
    ];
}

Answer for comments:

Honestly in my experience I didn't have that case, but if you want to redirect to the another view on failed login (for specific field 'estado'), you can customize the "sendFailedLoginResponse" method, and add some additional if-condition for checking the 'estado'. As the "sendFailedLoginResponse" method will be called only for getting failed login response instance, then you can check: is that fail comes from 'estado' field actually. Something like this:

protected function sendFailedLoginResponse(Request $request)
{
    // custom case, when login failed and estado is 2
    if ($request->get('estado') == 2) {
        return view('some.specific.view');
    }
    // laravel by default implementation
    else {
        throw ValidationException::withMessages([
            $this->username() => [trans('auth.failed')],
        ]);
    }
}

Remember, in this case (when we're redirecting the user to some page) we actually not redirecting as for always, but instead we're just returning a view. We do that because I think you don't want to let the users to open that specific view page anytime their want, as you need to let them see that page only for specific case. But when you'll do the actual redirect, then you will let the users to visit that page with some static URL. Of course, you can do some additional stuff (add something in DB or the Session, and check is the request comes actually from 'estado' fails, but not from any user), but this could be a headeche for you, and in my opinion that will not be a real laravel-specific code.

Anyway, this is the strategy. I don't think, that this is mandatory, but this can be do your work easy and secure.

Note: I've got this deafault implementations from "AuthenticatesUsers" trait (use use Illuminate\Foundation\Auth\AuthenticatesUsers;). In any time you can get some available methods from there and override them in your LoginController, as the LoginController used that as a trait.


推荐阅读