Show 404 if tenant doesn't exists
Hello everyone,
I am using subdomains for multi-tenancy in my Filamentphp v3 project.
The login pages for subdomains are like company-1.domain.com/dashboard/login
company-2.domain.com/dashboard/login
both company-1 and company-2 are registered as a tenant and the login page works good, but when I try to access company-3 subdomain which isn't registered, I still see the login page.
Is there a way to show 404 page if the tenant status is not active or it doesn't exist.
Thank you so much.
Solution:Jump to solution
```
public function handle(Request $request, Closure $next): Response
{
if (!Auth::check()) {
$slug = explode('.', $request->getHost())[0]; // Extract the subdomain...
11 Replies
bump
Natively we get a 404 with sub directory pages.
It sounds like you need to enforce access to registred domains only. Is your pimrary panel domains specific?
Hi @toeknee it is working with subdomains. I am not sure how to find out which one is primary panel because I have 2 panels 1 is admin and the other is dashboard where all other users accept admin can login.
I had to add a middleware in panel by using ->middleware and checked if the slug in tenants table exists otherwise abort(404) but I am not sure if this is the right way to do it.
That should be ok, i would ensure it's only added to the login route though as that will cuase an extra query per request.
@toeknee that is really good suggestion, is it possible to please let me know how do I make it work only on login, forgot pass, register routes ?
IU would say to ensure it is applied only on
->authMiddleware([])
and not ->middleware([])
well authMiddleware isn't working on login page which is why I had to use it on ->middleware
for example. when I was using company-4.domain.com/dashboard/login it worked because the authMiddleware didn't fire up but ->middleware() did
I supose you could do a quick check if they are logged in skip this check, else check?
Yes that is smart.
Thank you so much.
Solution
this worked.