gpt4 book ai didi

php - 如何在两个 laravel 项目之间使用 API 手动登录?

转载 作者:行者123 更新时间:2023-12-04 04:00:04 25 4
gpt4 key购买 nike

我有两个基于 Laravel 的项目,我想使用第一个项目提供的 API 登录,并在第二个项目中使用此身份验证。

在第二个项目的 LoginController 中:


public function login(Request $request)
{
$login_response = Http::post('{first_project_login_api}', [
'data' => [
"email" => $request->input('email'),
"password" => $request->input('password')
]
]);

if ($this->attemptLogin($login_response)) {
return $this->sendLoginResponse($request);
}
}

protected function attemptLogin(Response $response)
{
return $response->object()->status === 200;
}

在第二个项目中,我不需要数据库,因为我想在第一个项目中进行身份验证,但似乎不可能!

实际上我需要知道如何覆盖 LoginController 中的 attemptLogin() 函数。

如果有人能给我建议,我将不胜感激!😊

最佳答案

我不会在应用程序之间使用登录,而是使用 API key 。最简单的入门方法是使用简单的 API Authentication .

首先为用户表创建迁移。

Schema::table('users', function ($table) {
$table->string('api_token', 80)->after('password')
->unique()
->nullable()
->default(null);
});

要获取 key ,请在 Tinker、命令或创建中为用户设置它们。

$user->api_token = Str::random(60);
$user->save();

使用中间件保护您的 API 路由。

Route::middleware('auth:api')->group(function() {
// your routes
});

调用您的 api 就像这样简单。

response = $client->request('POST', $yourRoute, [
'headers' => [
'Authorization' => 'Bearer ' . $yourToken,
'Accept' => 'application/json',
],
]);

这是一个相当基本的设置,对于生产或前进你应该看看 SanctumPassport .这只是一个好的开始,我认为您是基于您的问题。

关于php - 如何在两个 laravel 项目之间使用 API 手动登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63215103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com