【问题标题】:How can i fix error InvalidArgumentException: Auth guard [customers] is not defined. in JWT如何修复错误 InvalidArgumentException: Auth guard [customers] 未定义。在智威汤逊
【发布时间】:2020-06-11 06:30:41
【问题描述】:

当我通过 jwt 登录 api 时出现错误 ..当我注册它的善意但登录显示错误时

InvalidArgumentException:未定义身份验证保护 [客户]。在 文件 C:\Users\Ahmed\Desktop\project web\laravel_pro\vendor\laravel\framework\src\Illuminate\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);
    }

这是api路由

Route::post('login','UserController@login');

这是 auth.php

 '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 config:clearphp artisan config:cache
  • 我试过之前芦荟不行,你有其他解决方案吗
  • 但在运行 php artisan config :cache ....错误更改为 Call to undefined method Illuminate\Auth\TokenGuard::attempt()
  • 将您的驱动程序从token 更改为session 'driver' => 'session', 之后再次运行php artisan cache:clear php artisan config:cache
  • 谢谢我搜索,我找到了从令牌的解决方案并更改为会话..但现在显示新错误 invalid_credentials .....我的代码是对还是错???

标签: laravel api jwt


【解决方案1】:

第一个猜测:在config/auth.php 你想要的守卫的驱动程序应该是'jwt'而不是'token',因为'token'驱动程序针对固定的users 表中的哈希值,而不是 jwt,它是易变的,不会查询数据库来找出用户。

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

关于错误

Auth guard [customers] 未定义。

检查这不是拼写错误,并且始终将其称为“客户”,因为“客户s”不是守卫,而是提供者。例如,检查路由中间件(例如Route::['middleware'=>'auth:customer']


话虽如此......

我猜你安装了tymondesigns/jwt-auth,它的配置指南指出了你替换api 防护以使用的场景,因此,在这种假设下,确保你已经实现了以下修改:

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

  2. 1234563用户表,而不是 jwt,它是易变的,不会查询数据库来找出用户。
  3. 你需要在你的Customer模型上实现JWTSubject接口(添加东西就行了,其他方法不用碰)

    使用 Tymon\JWTAuth\Contracts\JWTSubject;

    类 Customer 扩展 Authenticatable 实现 JWTSubject {

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    
    /**
     * Return a key value array, containing any custom claims 
     * to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    { 
        // just an example here, change it to whatever you want
        return [
           'picture' => 'picture', // array values are the fieldname in the DB
           'twitter' => 'twitter_url' 
        ];
    }
    ...
    
  4. 您的控制器应该在构造函数中实现相应的保护,并且将登录方法列入白名单,因为传入的标头中没有 JWT。

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

    公共函数登录() { $credentials = request(['email', 'password']); // 令牌从尝试中返回 if (!$token = auth()->attempt($credentials)) { return response()->json(['error' => 'Unauthorized'], 401); } 返回响应()->json([ 'access_token' => $token, 'token_type' => '承载者', 'expires_in' => auth()->factory()->getTTL() * 60 ]); } ...其他方法...

我会先抛弃显而易见的:

  • 您还能以用户身份登录吗(API 守卫)?如果这样做,请尝试将驱动程序更改为“jwt”

【讨论】:

    【解决方案2】:

    首先将你的后卫的drivertoken更改为session,然后运行以下命令

    php artisan cache:clear
    php artisan config:cache
    

    【讨论】:

    • 它修复的错误,但在邮递员登录时总是给出我的“错误”:“invalid_credentials”......但是我在注册密码时使用>Hash :: make
    • @ahmedaljabli Laravel 默认使用email 字段进行身份验证;您是否已采取措施使用用户名进行身份验证?
    • 我知道 Laravel 默认使用电子邮件,我使用电子邮件我不使用用户名
    猜你喜欢
    • 2021-04-26
    • 2015-10-16
    • 2021-07-15
    • 2016-09-21
    • 2017-01-30
    • 2020-12-27
    • 2015-05-25
    • 2015-04-03
    • 2023-03-10
    相关资源
    最近更新 更多