我正在使用laravel (v8)开发一个更大的web应用程序,因为项目可能会增长到预期的规模,所以我决定从一开始就将前端和后端分成两个不同的repos。
现在我想为前端实现用户身份验证,我现在的问题是我应该如何处理这个问题。当前端和后端在同一个repo中时,我知道从哪里开始,但像这样拆分,我似乎找不到一种方法来解决这个问题。
仅供参考:我已经为我的后端路由实现了基于令牌的身份验证
编辑:调整后的标题
发布于 2021-02-26 03:50:40
这非常简单,您可以向一个端点进行身份验证,我们将其命名为/ api / refresh_token,此端点将以json格式返回令牌、登录和用户,然后只需通过javascript将令牌保存到本地存储或其他位置(cookies、本地文件等),然后当您进行api调用时,只需使用保存的令牌来传递laravel api中间件,使用Laravel Passport和Laravel Responder包检查此登录示例代码
public function login(LoginRequest $request)
{
$validated = $request->validated();//array valid data
$credentials = request(['email', 'password']);
try {
$response = $this->client->post("{$this->oauth_server}/oauth/token", [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('auth.oauth_client_id'),
'client_secret' => config('auth.oauth_secret'),
'username' => $request->email,
'password' => $request->password,
],
]);
} catch (Exception $e) {
return responder()->error($e->getCode(), $e->getMessage())->respond();
}
return responder()->success([
"oauth" => json_decode((string)$response->getBody(), true)
])->respond();
}https://stackoverflow.com/questions/66374583
复制相似问题