如何在Laravel 5.6中使用cartalyst sentinel和tymon jwt?
我想知道如何使用Laravel 5.6中的Cartalyst Sentinel 2.0身份验证包实现tymon jwt 1.0.0 rc2,以利用节流和其他Sentinel功能 在AuthController中,我使用jwt auth Docs中提到的login()方法来验证凭据并生成令牌如何在Laravel 5.6中使用cartalyst sentinel和tymon jwt?,laravel,jwt,cartalyst-sentinel,Laravel,Jwt,Cartalyst Sentinel,我想知道如何使用Laravel 5.6中的Cartalyst Sentinel 2.0身份验证包实现tymon jwt 1.0.0 rc2,以利用节流和其他Sentinel功能 在AuthController中,我使用jwt auth Docs中提到的login()方法来验证凭据并生成令牌 public function login() { $credentials = request(['email', 'password']); if (! $token = auth()-
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = auth()->attempt($credentials))
return response()->json(['error' => 'Unauthorized'], 401);
return $this->respondWithToken($token);
}
我所做的如下
public function login()
{
$credentials = request(['email', 'password']);
if (! Sentinel::authenticate($credentials))
return response()->json(['error' => 'Unauthorized'], 401);
$token = auth()->attempt($credentials);
return $this->respondWithToken($token);
}
public function attempt(array $credentials = [], $login = true)
{
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
if ($this->hasValidCredentials($user, $credentials)) {
return $login ? $this->login($user) : true;
}
return false;
}
但我不认为这是正确的方法,因为存在双重身份验证,第一种是由Sentinel,第二种是由jwt。这对性能不利
第二种解决方法是修改驻留在vendor/tymon/jwt auth/src文件夹中的JWTGuard类中的trunt()方法
默认值如下所示
public function login()
{
$credentials = request(['email', 'password']);
if (! Sentinel::authenticate($credentials))
return response()->json(['error' => 'Unauthorized'], 401);
$token = auth()->attempt($credentials);
return $this->respondWithToken($token);
}
public function attempt(array $credentials = [], $login = true)
{
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
if ($this->hasValidCredentials($user, $credentials)) {
return $login ? $this->login($user) : true;
}
return false;
}
我就这样改了
public function attempt(array $credentials = [], $login = true)
{
if ($user = Sentinel::authenticate($credentials)) {
return $this->login($user);
}
return false;
}
我现在不知道这是否是一个正确的解决方案?在jwt配置中,使用此选项进行更改
'auth' => Tymon\JWTAuth\Providers\Auth\Sentinel::class,
在auth controller中,您可以使用此函数
use Tymon\JWTAuth\Facades\JWTAuth;
现在您可以登录了。只需使用
auth('api')->user()
即可让用户使用jwt或passport with sentinel谢谢您的回复,我将尝试一下。