簡體   English   中英

如何根據 Laravel Passport 客戶端 ID(或 user_id)從多個“用戶”表中對不同網站用戶進行身份驗證

[英]How to authenticate different websites users from multiple “users” tables, according to Laravel Passport client id (or user_id)

I'm using three different databases with my Laravel backend, basically, one is for the backend itself, a second one is for a foo.com Nuxt website and the third one is a bar.com Angular website. 這三個數據庫中的每一個都有一個如下所示的users表:

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

目前,各個網站都使用Laravel Passport對后端數據庫進行認證。 但是現在,我需要兩個網站foo.combar.com使用他們自己的數據庫users表進行身份驗證。

我的Laravel Passport oauth_clients配置如下所示:

+----+---------+------+---------------------------+----------+-----+
| id | user_id | name | secret | redirect         | provider | ... |
+----+---------+------+---------------------------+----------+-----+
|  1 |    null |  Foo | ****** | foo.com/callback |      foo | ... |
|  2 |    null |  Bar | ****** | bar.com/callback |      bar | ... |
+----+---------+------+---------------------------+----------+-----+

因此,目前,來自foo.com的用戶通過foo客戶端使用他們的登錄名/密碼登錄,來自bar.com的用戶通過bar客戶端使用他們的登錄名/密碼登錄,但兩者都在后端users表上。

如何強制Laravelbar.com用戶使用foo.users表,為foo.com用戶使用bar.users表? 另外,如何限制foo用戶使用foo中間件和bar用戶使用bar中間件?

我相信您可以通過使用 $_SERVER['HTTP_HOST'] 檢測請求主機名來實現這一點。

然后你必須為 3 個不同的域(.env、.foo.env 和 .bar.env)設置 3 個不同的 .env 文件,每個域都有不同的數據庫連接配置。

在您的 bootstrap/app.php 中,您可以添加以下代碼:

$env  = '.env';
if ($_SERVER['HTTP_HOST'] == 'foo.com') {
    $env  = '.foo.env';
}
else if ($_SERVER['HTTP_HOST'] == 'bar.com') {
    $env  = '.bar.env';
}
$app->loadEnvironmentFrom($env);

我相信這不是最好的選擇或最好的方法。 但我相信你可以用它實現你想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM