0
votes

All passport features works, passport is installed correct. The problem is somewhere where Laravel and Passport is connecting I guess, As I can't validate user's credentials upon login action. Validate is asking for request, if I am passing the incoming parameter request, validate method returns only true.

class AuthController extends Controller
{

     public function login(Request $request)
     {
          $login = $request->only(['username', 'password']);
          dd(Auth::validate($login));
     }

}

error:

Undefined index: request in file D:\oms\api\vendor\laravel\framework\src\Illuminate\Auth\RequestGuard.php on line 71

config/auth.php

'defaults' => [
    'guard' => 'api',
    'passwords' => 'users',
],
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
        'hash' => false,
    ],
],

class User extends Authenticatable
{
    use HasApiTokens,
    protected $fillable = [
       'username',
       'password',
    ];
}

Please, any tought will be fine :)

1
Did you look at the code the error message is pointing you to? Does it not suggest that you should be passing an array with a request element?miken32
Also, this is all done automatically by the login controller by importing the Illuminate\Foundation\Auth\AuthenticatesUsers trait. You shouldn't need to do this manually.miken32
I deleted that controller , now doing my own, if I pass a request to method, it returns always true;Mike

1 Answers

0
votes

I really recommend you to have look at Passport's documentation about Authenticating Users.

The auth()->validate($credentials) you've used is just validating the parameters in given to it, not authenticating a user.

You might be interested what Passport's LoginController implements for a login process:

public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

the method that actually do the login action is attemptLogin(...) which is as bellow:

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

TL,DR:

auth()->guard()->attempt($request->only(['username', 'password']));