[英]How to change the default table/model (User) for JWT authentication in Laravel and Dontrine?
I am working on a project that was built on Laravel 5.4.24
and integrated with Symfony
component and Doctrine
.我正在开发一个基于
Laravel 5.4.24
并与Symfony
组件和Doctrine
集成的项目。 I need to authenticate staffs on request from site_staffs
table (SiteStaff model)
and return the token.我需要根据
site_staffs
表(SiteStaff model)
请求对员工进行身份验证并返回令牌。 site_staffs
table has these columns: (id,
site_id
, code, number,..)
. site_staffs
表有这些列: (id,
site_id
, code, number,..)
。 Some columns have different names in the SiteStaff entity
(id,
site
, code,..)
.某些列在
SiteStaff entity
具有不同的名称(id,
site
, code,..)
。 There is another table for users
and I have no issue to authenticate users and return the token.还有另一个
users
表,我没有问题验证用户并返回令牌。 (In my case, I don't need to authenticate users and I just did that for testing). (就我而言,我不需要对用户进行身份验证,我只是为了测试而这样做)。 The issue is when I try to change the default settings of JWT to authenticate staffs from
site_staffs
table as below, I get an error message (see down the page):问题是当我尝试更改 JWT 的默认设置以验证来自
site_staffs
表的员工时,我收到一条错误消息(请参阅页面下方):
config/jwt.php:配置/jwt.php:
//'user' => 'App\User',
'user' => 'App\Entities\SiteStaff',
config/auth.php:配置/auth.php:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api_token' => [
'driver' => 'api_token',
'provider' => 'users',
],
//'api' => [
//'driver' => 'jwt',
//'provider' => 'users',
//],
],
'providers' => [
'users' => [
'driver' => 'doctrine',
//'model' => App\Entities\User::class,
'model' => App\Entities\SiteStaff::class,
],
pi/SiteStaffController: pi/站点员工控制器:
I am passing the credentials to JWT as follow:我将凭据传递给 JWT,如下所示:
$credentials =
[
'site' => $bpId,
'code' => $lanyard_code
];
if(! $token = JWTAuth::attempt($credentials)){
return response()->json(['error' => ['messages' =>
['Invalid credentials'] ]
], IlluminateResponse::HTTP_UNAUTHORIZED);
}else{
dd($token);
}
Here is the error I get:这是我得到的错误:
Type error: Argument 1 passed to
Tymon\JWTAuth\Providers\User\EloquentUserAdapter::__construct() must be an
instance of Illuminate\Database\Eloquent\Model, instance of
App\Entities\SiteStaff given, called in /vagrant/vendor/tymon/jwt-
auth/src/Providers/JWTAuthServiceProvider.php on line 126
I tried many different ways and solutions but non of them worked for me.我尝试了许多不同的方法和解决方案,但没有一个对我有用。 I appreciate if you could let me know what is wrong and what am I missing?
如果您能让我知道出了什么问题,我错过了什么,我将不胜感激? Your help is highly appreciated.
非常感谢您的帮助。
As per the requirement to change users table name for jwt auth in laravel need to follow two-step changes which I have tried.根据在 laravel 中更改 jwt auth 的用户表名的要求,需要遵循我尝试过的两步更改。
I have changed successfully login/authentication table from users to a customer with following steps.我已通过以下步骤成功地将登录/身份验证表从用户更改为客户。
step-1: change below details for providers第 1 步:更改以下提供商的详细信息
config/auth.php配置/auth.php
'providers' => [
// 'users' => [
// 'driver' => 'eloquent',
// 'model' => App\User::class,
// ],
'users' => [
'driver' => 'database',
'table' => 'customer',
],
],
step-2: change in jwt file步骤 2:更改 jwt 文件
config/jwt.php配置/jwt.php
// here to give a model path for authentication
'user' => 'App\Models\Customer',
Reference: change users table name for jwt auth in laravel参考: 在 Laravel 中更改 jwt auth 的用户表名
In my scenario I have two guards, web and api:在我的场景中,我有两个守卫,web 和 api:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'managers',
'hash' => false,
]
],
So, I follow these steps described in Tymon's Jwt Library docs:因此,我按照 Tymon 的 Jwt 库文档中描述的这些步骤进行操作:
"If the newly created 'api' guard is not set as a default guard or you have defined multiple guards to handle authentication, you should specify the guard when calling auth()." “如果新创建的 'api' 守卫未设置为默认守卫,或者您已定义多个守卫来处理身份验证,则应在调用 auth() 时指定守卫。”
$token = auth('api')->attempt($credentials);
In my custom api auth controller I had implemented login method as below and it works:在我的自定义 api auth 控制器中,我实现了如下登录方法并且它有效:
public function login(Request $request)
{
$this->validateLogin($request);
$credentials = $this->credentials($request);
$token = auth('api')->attempt($credentials);
return $token
? ['token' => $token]
: response()->json(['error' => \Lang::get('auth.failed')], 401);
}
Please, if this answer was useful for you, please give an upvote =).拜托,如果这个答案对你有用,请给个赞=)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.