【发布时间】:2020-04-20 21:02:17
【问题描述】:
我想创建一个使用用户角色编码的令牌。我试过查看文档,但我没有得到令牌。我已经尝试过。
我使用的是 laravel 5.8 和包版本 "tymon/jwt-auth": "^1.0.0-rc.2"
谢谢
授权控制器
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = auth()->guard('api')->attempt($credentials)) {
return response()->json(['errors' => 'In-valid username and Password'], 401);
}
$customClaims =[
'role' => auth('api')->user()->getRoleNames()
];
$payload = JWTFactory::make($customClaims);
$token = JWTAuth::encode($payload);
return $this->respondWithToken($token);
}
protected function respondWithToken($token)
{
return response()->json([
'success' => true,
'access_token' => $token,
'token_type' => 'bearer',
]);
}
【问题讨论】:
标签: laravel