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 关注
- 101 浏览
添加回答
举报