![](/img/trans.png)
[英]how to assign value of user_id from users table in cart in laravel?
[英]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.com
和bar.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
表上。
如何強制Laravel為bar.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.