Laravel v5.2.* (v5.2.29) Auth::guard('api')->attempt($user) 致命错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36534682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:35:15  来源:igfitidea点击:

Laravel v5.2.* (v5.2.29) Auth::guard('api')->attempt($user) fatal error

phplaravel

提问by Vanya Avchyan

Has anyone encountered this problem

有没有人遇到过这个问题

dd($this->user->check()); return false

but

Auth::guard('user')->attempt(App\User::find(1)) 

return the error

返回错误

Call to undefined method Illuminate\Auth\TokenGuard::attempt()

调用未定义的方法 Illuminate\Auth\TokenGuard::attempt()

Please help to resolve this problem.

请帮助解决这个问题。

回答by Vanya Avchyan

I solved this problem

我解决了这个问题

In config/auth.phpconfiguration:

config/auth.php配置中:

'user' => [
    'driver' => 'token',
    'provider' => 'userProvider',
],

We need to change to:

我们需要改为:

'user' => [
    'driver' => 'session',
    'provider' => 'userProvider',
],

Because Auth::guarddata is stored in the session

因为Auth::guard数据存储在会话中

And further work on the well-known scheme

以及对众所周知的方案的进一步工作

Auth::login() = Auth::guard('user')->login()
Auth::attempt() = Auth::guard('user')->attempt()
Auth::user() = Auth::guard('user')->user()
...

回答by MikielD.

To the function attemptyou shall pass an array of creditianals, not a user object.

attempt您应该向该函数传递一个creditianals 数组,而不是一个用户对象。

For example:

例如:

Auth::guard('user')->attempt([
    'email' => '[email protected]',
    'password' => '1234'
]);

And if you are using Laravel and need to authenticate the user, you can call similar:

如果您正在使用 Laravel 并且需要对用户进行身份验证,您可以调用类似的方法:

auth()->attempt([
    'email' => '[email protected]',
    'password' => '1234'
]);