我正在尝试使用 Laravel 11、Laravel Passport 和 TanancyForLaravel 实现登录功能,但出现以下错误:
Invalid key supplied
我想为租户的用户创建登录功能。每个租户都有自己的一组用户,他们应该只能由自己的租户登录。每个租户都有自己的数据库。
我的猜测是 Laravel 正在主数据库中查找密钥,而它应该在我的租户数据库中查找密钥。
我有以下config/auth.php:
'guards' => [
'tenant_web' => [
'driver' => 'session',
'provider' => 'tenant_users',
],
'tenant' => [
'driver' => 'passport',
'model' => 'tenants_users',
],
],
'providers' => [
'main_users' => [
'driver' => 'eloquent',
'model' => env('AUTH_MODEL', App\Models\User::class),
],
'tenant_users' => [
'driver' => 'tenant',
'model' => App\Models\Tenant\User::class,
],
],
我的 Controllers/AuthController.php 中的这条路线:
public function store(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required',
]);
$success = auth()->guard('tenant')->attempt([ //This is where the error is thrown
'email' => $request->input('email'),
'password' => $request->input('password'),
]);
if ($success) {
$token = auth()->user()->createToken('authToken') //Error is also thrown here
->accessToken;
$response = [
'success' => true,
'token' => $token
];
return response($response, 200);
} else {
$response = ["message" => "Password mismatch"];
return response($response, 422);
}
}
这条路线在这里被称为:
Route::middleware([
'api',
InitializeTenancyByDomain::class,
PreventAccessFromCentralDomains::class,
])->group(function () {
Route::post('/login', [AuthController::class, 'store']);
});
我尝试了很多不同的解决方案,遵循指南,并尝试了我能想到的一切。我一直遵循的主要指南是: 文字 其中说:
注意:不要使用passport:install 命令。该命令在中央应用程序中创建加密密钥和两个客户端。相反,我们稍后将生成密钥并手动创建客户端。
然后我做了以下事情:
要为整个应用程序生成单个 Passport 密钥对,请通过将以下代码添加到租户数据库播种器来为租户创建 Passport 客户端。
public function run()
{
$client = new ClientRepository();
$client->createPasswordGrantClient(null, 'Default password grant client', 'http://your.redirect.path');
$client->createPersonalAccessClient(null, 'Default personal access client', 'http://your.redirect.path');
}
您可以在 config/tenancy.php 文件中的 seeder_parameters 键处设置租户数据库 Seeder 类。 然后,通过运行 php artisan Passport:keys 为数据库播种并生成密钥对。
如果您需要更多信息,请询问,因为我真的需要答案。 谢谢你的一切!
这对我有用:
php artisan passport:install
之后运行:
php artisan passport:keys --force
然后:
sudo chmod -R 0777 ./storage