tenancy: Fortify Doesn't work with Tenancy Auth not getting user.
Describe the bug
Setup route middleware with
InitializeTenancyByDomain::class, 'web', 'universal', 'auth', 'verify', PreventAccessFromCentralDomains::class,
After login, session data is stored in both the central database and the tenant database, but it is not getting the auth user, returns null. So if it attempts to visit any route that requires verify, it just redirects back to {tenant}.{domain}/ route as an auth user doesn’t exist. Spent the last couple days on discord, following instruction on there I was able to get registration, and login working great, everything stores, but so far what information on discord is kind of a jumbled mess and nobody can’t quite figure out to make fortify work and if they have, they don’t post their solution.
I know this laravel fortify issue has been talked about, and everyone always gets sent to discord, but since there is no set answer in discord we need to address this here. Fortify is integral part of the Laravel ecosystem. If we can’t get this working correctly, what other auth system can we use that just works with tenancy. A multi-tenant system that doesn’t auth, kind of pointless.
Steps to reproduce
Install laravel/fortify Install Tenancy Setup tenant routes Make sure to
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain; use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;
in the proper locations
Expected behavior
I create the tenant in the backend of the central app, create a default user, user points their domain to {tenant}.{domain}/myadmin and they are presented with a login screen that logs them in to their admin.
Your setup
- Laravel version: [e.g. 8.4.7]
- stancl/tenancy version: [e.g. 3.4.^]
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 20 (6 by maintainers)
Hey @stancl , yep, this is an issue with the constructor DI:
For anyone else, because we use the domain/subdomain tenancy initialization with sanctum and fortify, this is what we did to get around this issue for now.
Basically just need to override how Fortify binds the
StatefulGuardclass into the container through its own register function.Because some of the tenant resolving and domain resolving code isn’t available to be called outside of their own respective classes, some of that code was copied into here. This doesn’t feel the greatest, but, it is only generally called first when Fortify is building it’s controller constructors. If tenancy is already initialized, it just continues on building up the guard without re-initializing.
Not sure if there’s a better way other than modifying how Laravel’s first party packages are doing things, but this at least makes it work on the sanctum, fortify side.
Edit: I should also note that this code is not accounting for ‘universal’ routes, so it’s essentially binding fortify routes that build the statefull guard with DI to the tenant context vs the central one. So, another solution of sorts would need to be worked out if the fortify routes are needed in both central and tenant
I have successfully managed to implement Fortify ( * v1.24.5 ) and Tenancy ( stancl/tenancy": "^3.8) with this thanks to @jhoopes comment and suggestion:
Is there any schedule for version 4 release?
You are right, I had to do another change to run it properly, but i am not sure where its gonna break next.
according to L11 doc I did change the sequence
bootstrap/providers.php (This file is automatically generated by Laravel, just change the sequence as below) return [ App\Providers\TenancyServiceProvider::class, App\Providers\AppServiceProvider::class, App\Providers\FortifyServiceProvider::class, App\Providers\JetstreamServiceProvider::class, ];
and in my app service providers boot method:
public function boot(): void { $domainIsTenant = (new Domain)->where(‘domain’, \request()->getHost());
With this i am able to get the right db connection injected in Database Session Handler
It’s been mostly stable for a few months, we’re working on adding some final features now. Hope to have everything done within Q1.
When can we expect version 4?
The issue is pretty much what’s described on the Early Identification page of v3 docs. We’re in the process of finishing the early identification logic in the upcoming v4 version.
Thanks @jhoopes Do we have an official solution for this? When
StatefulGuardDI is in any controller’s__construct, it is reading session from central, even though the route is under tenant middleware.This is not a bug, a bug would be something that works differently than described in the docs.
The issue with Fortify is that it uses constructor DI which the docs say isn’t supported.
In version 4 I might try to find a way to improve this, but currently everything is working as described, so this is still a support question.
You can try opening a PR in laravel/fortify to change all instances of controller constructor DI to route action DI. I don’t think Fortify should have to change this to support other packages, but practically it’s just a few lines of code so they might be open to it for better compatibility with the ecosystem.
There are many auth systems. laravel/ui, Breeze, Jetstream, and many more. The first two should work with tenancy perfectly.
Nothing’s stopping you from using auth. You just can’t use constructor DI or packages that use constructor DI.
I personally wouldn’t be using packages for this, I’d just make a simple Livewire component and implement auth as needed for each project individually.