我想在登录后使用令牌传递用户的角色。而不是像下面这样传递
return response()->json([
'success' => true,
'access_token' => $token,
'token_type' => 'bearer',
'role' => auth('api')->user()->getRoleNames()
/* 'expires_in' => auth('api')->factory()->getTTL() * 60 */
]);
我想使用 JWT 令牌对用户的角色进行编码。我试过的
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = auth()->guard('api')->attempt($credentials)) {
return response()->json(['errors' => 'In-valid username and Password'], 401);
}
return $this->respondWithToken($token);
}
protected function respondWithToken($token)
{
$role =auth('api')->user()->getRoleNames();
$payload = JWTFactory::make($role);
$token = JWTAuth::encode($payload);
return response()->json([
'success' => true,
'access_token' => $token,
'token_type' => 'bearer',
]);
}
当我尝试这个时,我没有得到令牌。我已经看过这个文档https://github.com/tymondesigns/jwt-auth/wiki/Creating-Tokens来做到这一点。
慕妹3146593