1 回答

TA貢獻1820條經驗 獲得超9個贊
在這里,我們需要使用用戶角色創建自己的令牌。對我有用的代碼可能對其他人有幫助。
public function login()
{
$credentials = request(['email', 'password']);
if (!auth()->guard('api')->claims(['role' => 'bar'])->attempt($credentials)) {
return response()->json(['errors' => 'In-valid username and Password'], 401);
}
$token = auth('api')->claims(['role' => str_replace(['["', '"]', '/', '*'], '',auth('api')->user()->getRoleNames())])->attempt($credentials);
return $this->respondWithToken($token);
}
protected function respondWithToken($token)
{
auth('api')->payload($token);
return response()->json([
'success' => true,
'access_token' => $token,
'token_type' => 'bearer',
]);
}
- 1 回答
- 0 關注
- 116 瀏覽
添加回答
舉報