【问题标题】:Auth::attempt() trowing an error saying "Call to undefined method Illuminate\Auth\TokenGuard::attempt()" in Laravel 6.4Auth::attempt() 在 Laravel 6.4 中出现错误提示“调用未定义的方法 Illuminate\Auth\TokenGuard::attempt()”
【发布时间】:2020-02-26 00:43:00
【问题描述】:

最近我试了一下最新的 Laravel(6.4)。试图实现基于 API 的简单登录功能。没有使用任何包,如 Passport 或 Tymon 的 JWT。我使用了非常基本的身份验证(只是在用户表中保留一个名为“api_token”的列)。但是遇到错误说

Symfony\Component\Debug\Exception\FatalThrowableError : 调用未定义的方法 Illuminate\Auth\TokenGuard::attempt()

这是我的控制器

//only method in the controller, so used `__invoke` method.
    public function __invoke(Request $request)
    {
         $request->validate([
              'email'    => 'required',
              'password' => 'required',
         ]);

         $credentials = $request->only(['email', 'password']);
         $credentials['active'] = 1;

         if (Auth::guard('api')->attempt($credentials)) {
              return response()->json([
                  'data' => 'Wrong credential given'
              ]);
         }

         return response()->json([
             'data' => \auth()->user()
         ]);
    }

这是我的config/auth.php 文件状态。我正在使用 api guard 进行访问。但是令牌应该在登录后出现。在这里,我无法登录。

    'defaults' => [
            'guard' => 'api',
            'passwords' => 'users',
        ],

        'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],

            'api' => [
                'driver' => 'token',
                'provider' => 'users',
                'hash' => false,
            ],
        ],
    ]

【问题讨论】:

  • 不,它没有。我正在提供合适的后卫和提供者。但它仍然显示我提到的错误
  • 你可以使用Auth::once($credentials),在this docs page上搜索Authenticate A User Once 。 `。 您可以使用 once 方法将用户登录到应用程序的单个请求。不会使用任何会话或 cookie,这意味着此方法在构建无状态 API 时可能会有所帮助
  • 现在它显示“调用未定义的方法 Illuminate\Auth\TokenGuard::once()”

标签: php laravel


【解决方案1】:

API Guard 没有attempt 方法,也没有任何其他形式的用户登录。它所做的只是保护路线。要对用户进行身份验证,您仍应使用默认保护。

【讨论】:

  • 在没有授权保护的情况下尝试过。找到相同的结果
  • 您的意思是您尝试使用 Auth::attempt() 并且仍然出现该错误?
  • 是的,这是因为默认的守卫。应该是网络
  • 是的,你是对的,API Guard 没有尝试方法
【解决方案2】:

当我将默认保护设置为'web' 时,问题就解决了(就像之前一样)。正如亚历克在他的回答中提到的那样,The API Guard does not have an attempt method。我认为api 需要在那里进行 API 调用。但是当您要授权用户时需要它。但在此之前,您可以使用默认的web 守卫来处理登录和注册。

所以,这是我最后的改动。 控制器:

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

    $credentials = $request->only(['email', 'password']);
    $credentials['active'] = 1;

    if (Auth::attempt($credentials)) {
        return response()->json([
            'data' => auth()->user()
        ]);
    }

    return response()->json([
        'data' => 'Wrong credential given'
    ]);

这是我的 config/auth.php 文件

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
],

【讨论】:

    【解决方案3】:

    对我有用的是像这样将 config/auth 文件中的警卫驱动程序从令牌更改为会话

    'guards' => [
        'staff' => [
            'driver' => 'session',
            'provider' => 'my_provider',
            'hash' => false,
        ]
    ]
    

    这样一来,这种尝试就像变魔术一样奏效了。现在我可以像这样使用这个中间件对用户进行身份验证

    Auth::guard('staff')->attempt([
        'email' => $r->email, 
        'password' => $r->password
    ]);
    

    像这样访问登录用户的信息

    auth('staff')->user();
    

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-06
      • 2018-08-08
      • 1970-01-01
      • 2023-03-05
      • 2019-12-21
      • 2017-05-11
      • 2018-07-30
      • 2017-03-10
      相关资源
      最近更新 更多