我曾在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非常赞赏。提前致谢。
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',
// ],
],
尝试清除缓存配置或php artisan config:clear
重建它php artisan config:cache
你将不得不也加入这个应用程式的AuthServiceProvider
的引导()方法扩展身份验证:
public function boot()
{
$this->registerPolicies();
Auth::extend('customers', function ($app, $name, array $config) {
return new CustomersGuard();
});
}