'Make login from central domain and redirect to tenant with tenancyforlaravel package
I want to make multitenant solution with multiple databases (one DB per tenant). Central domain for instance example.com has things like registration form and login form e.g example.com/sign-in Each customer is a new tenant identified by sub domain e.g foo.example.com
Sign in process (happy scenario)
- User see view called e.g sign-in on central domain
- User fill email and password
- System check if credentials are correct
- System will redirect authenticated user to his tenant domain e.g foo.example.com
- System will show dashboard view with data of that authenticated user. Dashboard is behind auth middleware.
I have decided to use this library: https://tenancyforlaravel.com/ Everything is working, I have created tenant, central domain is also working. My issue is, that if I want to log user from central domain, after redirection to his tenant domain, I get 403 Unauthenticated.
Here is a example code of tenant.php
Route::middleware([
'web',
InitializeTenancyBySubdomain::class,
PreventAccessFromCentralDomains::class,
])->group(function () {
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', function () {
return 'Tenant' . tenant('id');
});
});
});
I put Auth::Routes(); into web.php. I am using Laravel's UI Auth.
What I am missing? Thank you very much for help.
Solution 1:[1]
First of all you need to use feature "synced resources between tenants" be able to keep your user data synced between central and tenant DB's: https://tenancyforlaravel.com/docs/v3/synced-resources-between-tenants/
Then user auth could be done via "Impersonation" feature: https://tenancyforlaravel.com/docs/v3/features/user-impersonation/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yuriy Marad |
