我在L5.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
}
当我点击此登录时,而不是客户表,Auth从用户表!!
完成我还尝试使用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'
]
我添加了:
bundler/setup
令牌驱动程序或自定义驱动程序是否有任何更改。或者如何定义自定义Auth驱动程序?
任何帮助/指导都会非常感激。提前谢谢。
答案 0 :(得分: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',
// ],
],
答案 1 :(得分:0)
尝试清除配置缓存R.pop_back(node)
或重建它php artisan config:clear
答案 2 :(得分:0)
您还必须通过将身份验证添加到应用程序AuthServiceProvider
的boot()方法中来扩展身份验证:
public function boot()
{
$this->registerPolicies();
Auth::extend('customers', function ($app, $name, array $config) {
return new CustomersGuard();
});
}