【问题标题】:Laravel Passport Undefined index: request in fileLaravel Passport 未定义索引:文件中的请求
【发布时间】:2021-03-17 15:22:05
【问题描述】:

所有护照功能都有效,护照安装正确。 我猜问题出在 Laravel 和 Passport 连接的地方, 因为我无法在登录操作时验证用户的凭据。 验证是请求请求,如果我传递传入的参数请求,验证方法只返回true。

class AuthController extends Controller
{

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

}

错误:

未定义的索引:文件中的请求 D:\oms\api\vendor\laravel\framework\src\Illuminate\Auth\RequestGuard.php 在第 71 行

配置/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',
    ];
}

拜托,随便什么都好:)

【问题讨论】:

  • 您查看错误消息指向您的代码了吗?这不是建议您传递带有request 元素的数组吗?
  • 此外,这一切都是由登录控制器通过导入 Illuminate\Foundation\Auth\AuthenticatesUsers 特征自动完成的。您不需要手动执行此操作。
  • 我删除了那个控制器,现在我自己做,如果我将 request 传递给方法,它总是返回 true;
  • 我认为你可以在 'api' 守卫中这样调用。 Auth::guard('web')->validate(['email' => 'john@example.com', 'password' => '12345678']);

标签: php laravel api laravel-passport


【解决方案1】:

我真的建议你看看Passport's documentation about Authenticating Users

您使用的auth()->validate($credentials) 只是验证给定的参数,而不是验证用户。

您可能对 Passport 的 LoginController 为登录过程实现的内容感兴趣:

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);
    }

实际执行登录操作的方法是attemptLogin(...) 如下:

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

TL,DR:

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

【讨论】:

  • 当我添加尝试而不是验证时,我得到了错误:方法 Illuminate\Auth\RequestGuard::attempt 不存在。在文件 D:\oms\api\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php 第 103 行
  • 发生这种情况是因为我的默认守卫是 API 而不是 WEB 像往常一样
【解决方案2】:

看起来 Auth::validate() 仅适用于“网络”防护。 所以,如果你在 'api' 守卫里面,你可以像这样验证:

$result = Auth::guard('web')->validate(['email' => 'john@example.com', 'password' => '12345678']);
var_dump($result);

【讨论】:

    猜你喜欢
    • 2020-08-21
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2018-08-02
    • 2012-08-24
    • 2015-03-23
    相关资源
    最近更新 更多