laravel RequestGuard::attempt 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49443023/
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 17:33:06  来源:igfitidea点击:

RequestGuard::attempt does not exist

laravellaravel-5laravel-4laravel-5.2laravel-5.1

提问by Pranav Mandlik

I am trying multiguard authentication for api when i login for admin i am getting following error

当我为管理员登录时,我正在尝试对 api 进行多重保护身份验证,但出现以下错误

BadMethodCallException Method Illuminate\Auth\Req uestGuard::attempt does not exist.

BadMethodCallException 方法 Illuminate\Auth\Req uestGuard::attempt 不存在。

here is my login method in controller

这是我在控制器中的登录方法

 public function login(Request $request){
    if(Auth::guard('admin-api')->attempt(['email' => request('email'), 'password' => request('password')]))
    {

  // if successful, then redirect to their intended location


        $user = Auth::guard('admin-api');
        $success['token'] =  $user->createToken('admin')->accessToken;
        return response()->json(['success' => $success], $this->successStatus);
    }
    else{
        return response()->json(['error'=>'Unauthorised'], 401);
    }
}

my api.php

我的 api.php

Route::prefix('admin')->group(function () {


Route::post('login', 'API\Admin\AdminController@login')->name('admin.login');
Route::post('register', 'API\Admin\AdminController@register')->name('admin.register');

Route::group(['middleware' => 'auth:admin-api'], function(){
 Route::post('get-details', 'API\Admin\AdminController@getDetails');
});


});

my admin model

我的管理模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;

class Admin extends Authenticatable
{
 use HasApiTokens, Notifiable;
 protected $table = 'admin';
 protected $guard = 'admin-api';
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

}

}

please tell me any other inputs you want

请告诉我你想要的任何其他输入

回答by Salah

A better hack is this

一个更好的黑客是这个

   $credentials = ['email' => $request->username, 'password' => $request->password];

   //Since we are not using guard web in API request, we have to add it here ( 
   // guard('web') ) to access the Auth::attempt function,
   // the Auth::attempt needs web guard and crsf token, but using it here bypasses 
   // the post with crsf.
    if (Auth::guard('web')->attempt($credentials, false, false)) {
    dd('user is OK');
    }else{
   dd('user is NOT OK');
    }

回答by Miracool

Unfortunately the Laravel Facade for Auth does not expect you to use it for the api guard since sessions and cookies will be set, Thus does not support ->attempt()function. But the API middle-ware disables session and cookies since it is stateless. So here is the hack. Get to your confi\auth and create a similar web instance for your api guards thus

不幸的是,Laravel Facade for Auth 不希望您将其用于 api 防护,因为将设置会话和 cookie,因此不支持->attempt()功能。但是 API 中间件禁用会话和 cookie,因为它是无状态的。所以这里是黑客。进入您的 confi\auth 并为您的 api 守卫创建一个类似的网络实例

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

    'drivers-web' => [
        'driver' => 'session',
        'provider' => 'drivers',
    ],

    'api' => [
        'driver' => 'passport',//previously "token"
        'provider' => 'users',//This will be switched regularly from the middleware between drivers and users
    ],

    'drivers-api' => [
        'driver' => 'passport',//previously "token"
        'provider' => 'drivers',
    ],
],

Never the less, You can use passport to generate your client access tokens which are stateless sessions. And also serve well for authentication.

无论如何,您可以使用通行证来生成无状态会话的客户端访问令牌。并且也很好地用于身份验证。

回答by Charles Brocchiero

To me, I changed drivers for the web from passport to session. Laravel's cache had to be cleared to be able to go back to the session driver

对我来说,我将网络驱动程序从护照更改为会话。必须清除 Laravel 的缓存才能返回会话驱动程序

php artisan cache:clear