Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 如何修复错误InvalidArgumentException:Auth guard[customers]未定义。在JWT中_Laravel_Api_Jwt - Fatal编程技术网

Laravel 如何修复错误InvalidArgumentException:Auth guard[customers]未定义。在JWT中

Laravel 如何修复错误InvalidArgumentException:Auth guard[customers]未定义。在JWT中,laravel,api,jwt,Laravel,Api,Jwt,我在jwt登录api时出错。当我注册它的善意时,但在登录显示错误 InvalidArgumentException:未定义身份验证保护[customers]。在里面 文件C:\Users\Ahmed\Desktop\project web\laravel\u pro\vendor\laravel\framework\src\light\Auth\AuthManager.php 在线84 这是控制器 public function login(Request $request){ $cre

我在jwt登录api时出错。当我注册它的善意时,但在登录显示错误

InvalidArgumentException:未定义身份验证保护[customers]。在里面 文件C:\Users\Ahmed\Desktop\project web\laravel\u pro\vendor\laravel\framework\src\light\Auth\AuthManager.php 在线84

这是控制器

public function login(Request $request){
    $credentials = $request->only('email', 'password');

    if (Auth::guard('customer')->attempt($credentials))
    {
       $user= JWTAuth::guard('customer')->user(); 
       $jwt = JwtAuth::generateToken($user);

       return response()->json(compact('jwt'));

    }else{

        return response()->json(['error' => 'invalid_credentials'], 400);
    }
Route::post('login','UserController@login');
 'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

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

        'customer'=>[
          'driver' => 'token',
          'provider'=> 'customers',
        ]

    ],

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'customers' => [
            'driver' => 'eloquent',
            'model' => App\Customer::class,
        ],
这是api路线

public function login(Request $request){
    $credentials = $request->only('email', 'password');

    if (Auth::guard('customer')->attempt($credentials))
    {
       $user= JWTAuth::guard('customer')->user(); 
       $jwt = JwtAuth::generateToken($user);

       return response()->json(compact('jwt'));

    }else{

        return response()->json(['error' => 'invalid_credentials'], 400);
    }
Route::post('login','UserController@login');
 'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

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

        'customer'=>[
          'driver' => 'token',
          'provider'=> 'customers',
        ]

    ],

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'customers' => [
            'driver' => 'eloquent',
            'model' => App\Customer::class,
        ],
这是auth.php的

public function login(Request $request){
    $credentials = $request->only('email', 'password');

    if (Auth::guard('customer')->attempt($credentials))
    {
       $user= JWTAuth::guard('customer')->user(); 
       $jwt = JwtAuth::generateToken($user);

       return response()->json(compact('jwt'));

    }else{

        return response()->json(['error' => 'invalid_credentials'], 400);
    }
Route::post('login','UserController@login');
 'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

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

        'customer'=>[
          'driver' => 'token',
          'provider'=> 'customers',
        ]

    ],

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'customers' => [
            'driver' => 'eloquent',
            'model' => App\Customer::class,
        ],

请任何人帮助我解决问题。谢谢你首先将你的警卫的
驱动程序
令牌更改为
会话
,然后运行以下命令

php artisan cache:clear
php artisan config:cache

首先将警卫的
驱动程序
令牌
更改为
会话
,然后运行以下命令

php artisan cache:clear
php artisan config:cache

First guess:在
config/auth.php
中,所需防护的驱动程序应该是“jwt”而不是“token”,因为“token”驱动程序针对用户表中的固定哈希工作,而不是易失性的且不会查询数据库来找出用户的
jwt

第二个猜测:您对“内部用户”(“api”防护)和“客户用户”(应使用“客户”防护)使用相同的控制器。这令人困惑。您在哪里区分用户类型

关于错误

未定义Auth guard[客户]

检查它是否为打字错误,并且您始终将其称为“客户”,因为“客户s”不是警卫,而是提供者。例如,检查路由中间件(例如,
route::['middleware'=>'auth:customer']


已经说过……

我猜您已经安装了,其配置指南指向了一个场景,在该场景中,您将替换
api
guard来使用,因此,在该假设下,请确保您已经实现了以下修改:

  • 如果使用Laravel 5.4或更低版本,则需要。在较新版本中,它们会为您处理,因此您可以跳过此步骤

  • 正如我上面所说的,在
    config/auth.php
    中,所需防护的驱动程序应该是“jwt”而不是“token”,因为“token”驱动程序针对用户表中的固定散列工作,而不是
    jwt
    ,后者是易变的,不会查询数据库来找出用户

  • 您需要在
    Customer
    模型上实现
    JWTSubject
    界面(只需添加内容,无需触摸其他方法)

    使用Tymon\JWTAuth\Contracts\JWTSubject

    类Customer extends Authenticatable实现JWTSubject {

  • 您的控制器应该在构造函数中实现相应的保护,并将登录方法列入白名单,因为传入的头中没有JWT

    //用户控制器 公共函数构造() { $this->middleware('auth:users',['except'=>['login']]); }

    公共函数登录() { $credentials=请求(['email','password']); //令牌从尝试返回 如果(!$token=auth()->尝试($credentials)){ return response()->json(['error'=>'Unauthorized'],401); } 返回响应()->json([ “访问令牌”=>$token, '令牌类型'=>'承载', 'expires_in'=>auth()->factory()->gettl()*60 ]); } …其他方法

  • 首先,我要抛弃显而易见的:

    • 您仍然可以作为用户(api guard)登录吗?如果可以,请尝试将驱动程序更改为“jwt”

    First guess:在
    config/auth.php
    中,所需保护的驱动程序应该是“jwt”而不是“token”,因为“token”驱动程序针对用户表中的固定哈希工作,而不是易失性的、不查询数据库以找出用户的
    jwt

    第二次猜测:您对“内部用户”(“api”guard)和“客户用户”(应该使用“客户”guard)使用相同的控制器。这令人困惑。您在哪里区分用户类型

    关于错误

    未定义Auth guard[客户]

    检查它是否为打字错误,并且您始终将其称为“客户”,因为“客户s”不是一个守卫,而是一个提供者。例如,检查路由中间件(例如
    route:['middleware'=>'auth:customer']


    已经说过……

    我猜您已经安装了,其配置指南指向了一个场景,在该场景中,您将替换
    api
    guard来使用,因此,在该假设下,请确保您已经实现了以下修改:

  • 如果使用Laravel 5.4或更低版本,则需要。在较新版本中,它们会为您处理,因此您可以跳过此步骤

  • 正如我上面所说的,在
    config/auth.php
    中,所需防护的驱动程序应该是“jwt”而不是“token”,因为“token”驱动程序针对用户表中的固定散列工作,而不是
    jwt
    ,后者是易变的,不会查询数据库来找出用户

  • 您需要在
    Customer
    模型上实现
    JWTSubject
    界面(只需添加内容,无需触摸其他方法)

    使用Tymon\JWTAuth\Contracts\JWTSubject

    类Customer extends Authenticatable实现JWTSubject {

  • 您的控制器应该在构造函数中实现相应的保护,并将登录方法列入白名单,因为传入的头中没有JWT

    //用户控制器 公共函数构造() { $this->middleware('auth:users',['except'=>['login']]); }

    公共函数登录() { $credentials=请求(['email','password']); //令牌从尝试返回