添加用户角色到jwt,laravel 5 jwt-auth
Add the user role to the jwt, laravel 5 jwt-auth
我有一个 laravel 5 后端,它在使用 jwt-auth.
登录时发送一个 jwt-token 作为 json 响应
现在我想将用户角色添加到laravel发送的jwt令牌中,我尝试了以下方式:
这是我现在的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Database\Eloquent\Model;
use App\User;
class AuthenticateController extends Controller
{
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');
$user = User::where('email', '=', $credentials['email'])->first();
$customClaims = ['role' => $user->role];
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials, $customClaims)) {
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'));
}
}
?>
有没有更简洁的方法来做到这一点?
很奇怪,指定一些 $customClaims 作为 attempt() 方法的第二个参数实际上对我有用。
但是,您是否尝试过使用 JWTFactory Facade?它可以让您创建您想要的各种令牌。
您可以在此处获取更多信息:JWT-Auth Creating Tokens Page。
作为 installation guide suggests,在安装 JWT-Auth 时不要忘记将其添加为 Facade!
希望对您有所帮助!
目前的JWTAuth[0.5.9]稳定版似乎没有更简洁的方法。
正如作者在他的评论中指出的那样,他的下一个版本将对这个问题进行改进。你可以看看他的 develop 分支。
您目前正在查询用户两次,一次是为了获得角色而使用电子邮件,第二次是在 jwt::attempt()
method.I 中建议将查询减少到一个但做身份验证 {Auth::attempt($credientials)} 然后
将检索到的用户与自定义声明一起传递给 JWT::fromUser()
方法。所以
JWT::fromUser($user,['role' => $user->role])
JWT v1.0
的文档引起混淆。
将此方法添加到您的用户模型,以便您可以在对用户进行身份验证时添加自定义声明:
public function getJWTCustomClaims()
{
return = [
'type' => 'driver',
'id' => $this->driver->id
];
}
我有一个 laravel 5 后端,它在使用 jwt-auth.
登录时发送一个 jwt-token 作为 json 响应现在我想将用户角色添加到laravel发送的jwt令牌中,我尝试了以下方式:
这是我现在的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Database\Eloquent\Model;
use App\User;
class AuthenticateController extends Controller
{
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');
$user = User::where('email', '=', $credentials['email'])->first();
$customClaims = ['role' => $user->role];
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials, $customClaims)) {
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'));
}
}
?>
有没有更简洁的方法来做到这一点?
很奇怪,指定一些 $customClaims 作为 attempt() 方法的第二个参数实际上对我有用。
但是,您是否尝试过使用 JWTFactory Facade?它可以让您创建您想要的各种令牌。
您可以在此处获取更多信息:JWT-Auth Creating Tokens Page。
作为 installation guide suggests,在安装 JWT-Auth 时不要忘记将其添加为 Facade!
希望对您有所帮助!
目前的JWTAuth[0.5.9]稳定版似乎没有更简洁的方法。
正如作者在他的评论中指出的那样,他的下一个版本将对这个问题进行改进。你可以看看他的 develop 分支。
您目前正在查询用户两次,一次是为了获得角色而使用电子邮件,第二次是在 jwt::attempt()
method.I 中建议将查询减少到一个但做身份验证 {Auth::attempt($credientials)} 然后
将检索到的用户与自定义声明一起传递给 JWT::fromUser()
方法。所以
JWT::fromUser($user,['role' => $user->role])
JWT v1.0
的文档引起混淆。
将此方法添加到您的用户模型,以便您可以在对用户进行身份验证时添加自定义声明:
public function getJWTCustomClaims()
{
return = [
'type' => 'driver',
'id' => $this->driver->id
];
}