Laravel 5.4验证后卫司机[客户]没有定义

问题描述 投票:0回答:3

我曾在15.2实现的自定义身份验证。我曾跟随那些相同的步骤,但是,我不能够登录/注册自定义身份验证。以下是设置:

auth.php我添加客户定制的身份验证:

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

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

    'customers' => [
        'driver' => 'jwt',
        'provider' => 'customers',
    ],


],

//提供商科

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

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

然后在routes/api.php我说从middleware删除RouteServiceProvider.php后,下面的代码

Route::group(['middleware' => 'customers'], function() {

    Route::post('login', 'JwtAuth\LoginController@login'); //Had made new auth for JWT
}

当我打这个登录,而不是Customer表,验证从用户表做!

我也试图与以下内Controller\JwtAuth\LoginController.php代码:

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');
    $customer = Auth::guard('customers')->attempt($credentials);

    try {
        // attempt to verify the credentials and create a token for the user
        if (!$token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token'], 500);
    }
    // all good so return the token
    return response()->json(compact('token'), Response::HTTP_OK);
}

此代码抛出错误为:

Auth guard driver [customers] is not defined.

在我的下\App\Http\Kernel.php protected $middlewareGroups我又增加了:

'api' => [
        'throttle:60,1',
        'bindings'
    ],

'customers' => [
        'throttle:60:1',
        'bindings'
    ]

是否有令牌驱动程序或自定义驱动程序的任何变化。或如何定义自定义验证的驱动程序?

任何帮助/指导将b非常赞赏。提前致谢。

php laravel authentication jwt laravel-5.4
3个回答
0
投票
Auth Guard driver is defined in config/auth.php 

如下图所示

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

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

        'customers' => [
            'driver' => 'jwt',
            'provider' => 'customers',
        ],
    ],

还可以添加像供应商

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

    'customers' => [
        'driver' => 'eloquent',
        'model' => App\Models\Customer::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

0
投票

尝试清除缓存配置或php artisan config:clear重建它php artisan config:cache


0
投票

你将不得不也加入这个应用程式的AuthServiceProvider的引导()方法扩展身份验证:

public function boot()
{
    $this->registerPolicies();

    Auth::extend('customers', function ($app, $name, array $config) {
        return new CustomersGuard();
    });
}

documentation for adding custom guards

© www.soinside.com 2019 - 2024. All rights reserved.