Laravel 5.4 Auth 保护驱动程序 [customers] 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42503062/
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
Laravel 5.4 Auth guard driver [customers] is not defined
提问by Tarunn
I had implemented custom auth in L5.2. I had followed those same steps but, I am not able to login/signup with custom auth. Following is the setup:
我在 L5.2 中实现了自定义身份验证。我遵循了相同的步骤,但是,我无法使用自定义身份验证登录/注册。以下是设置:
in auth.php
i added customerscustom auth:
在auth.php
我添加了客户自定义身份验证:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'customers' => [
'driver' => 'jwt',
'provider' => 'customers',
],
],
// Providers Section
// 提供者部分
providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
],
Then in routes/api.php
I added following code after removing middleware
from RouteServiceProvider.php
然后,在routes/api.php
我删除后添加以下代码middleware
从RouteServiceProvider.php
Route::group(['middleware' => 'customers'], function() {
Route::post('login', 'JwtAuth\LoginController@login'); //Had made new auth for JWT
}
When I hit this login, instead of Customer table, Auth is done from User table!!
当我点击这个登录时,而不是 Customer 表,Auth 是从 User 表中完成的!!
I also tried with following code inside Controller\JwtAuth\LoginController.php
:
我还尝试在里面使用以下代码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);
}
This code throws error as:
此代码引发错误为:
Auth guard driver [customers] is not defined.
In my \App\Http\Kernel.php
under protected $middlewareGroups
i had added:
在我的\App\Http\Kernel.php
下protected $middlewareGroups
我补充说:
'api' => [
'throttle:60,1',
'bindings'
],
'customers' => [
'throttle:60:1',
'bindings'
]
Is there any change in token driver or custom driver. Or how to define custom Auth driver?
令牌驱动程序或自定义驱动程序是否有任何变化。或者如何定义自定义身份验证驱动程序?
Any help/guidance would b much appreciated. Thanks in advance.
任何帮助/指导将不胜感激。提前致谢。
回答by Samar Haider
Auth Guard driver is defined in config/auth.php
Like below
像下面
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
'customers' => [
'driver' => 'jwt',
'provider' => 'customers',
],
],
and also add in providers like
并添加像这样的提供者
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Models\Customer::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
回答by arhakim
Try to clear the config cache php artisan config:clear
or rebuild it php artisan config:cache
尝试清除配置缓存php artisan config:clear
或重建它php artisan config:cache
回答by Jannie Theunissen
You will have to also extend your authentication by adding this to the boot() method of your app's AuthServiceProvider
:
您还必须通过将其添加到应用程序的 boot() 方法来扩展您的身份验证AuthServiceProvider
:
public function boot()
{
$this->registerPolicies();
Auth::extend('customers', function ($app, $name, array $config) {
return new CustomersGuard();
});
}