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:
``` public function handle(Request $request, Closure $next): Response { if (!Auth::check()) { $slug = explode('.', $request->getHost())[0]; // Extract the subdomain...
Jump to solution
11 Replies
mohdaftab
mohdaftabOP2w ago
bump
toeknee
toeknee2w ago
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?
mohdaftab
mohdaftabOP2w ago
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.
toeknee
toeknee2w ago
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.
mohdaftab
mohdaftabOP2w ago
@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 ?
toeknee
toeknee2w ago
IU would say to ensure it is applied only on ->authMiddleware([]) and not ->middleware([])
mohdaftab
mohdaftabOP2w ago
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
toeknee
toeknee2w ago
I supose you could do a quick check if they are logged in skip this check, else check?
mohdaftab
mohdaftabOP2w ago
Yes that is smart. Thank you so much.
Solution
mohdaftab
mohdaftab2w ago
public function handle(Request $request, Closure $next): Response
{
if (!Auth::check()) {
$slug = explode('.', $request->getHost())[0]; // Extract the subdomain

// Check if the tenant exists
if (!Company::where('slug', $slug)->exists()) {
throw new NotFoundHttpException(); // Show 404 page if not found
}

return $next($request);
}

return $next($request);
}
public function handle(Request $request, Closure $next): Response
{
if (!Auth::check()) {
$slug = explode('.', $request->getHost())[0]; // Extract the subdomain

// Check if the tenant exists
if (!Company::where('slug', $slug)->exists()) {
throw new NotFoundHttpException(); // Show 404 page if not found
}

return $next($request);
}

return $next($request);
}
mohdaftab
mohdaftabOP2w ago
this worked.
Want results from more Discord servers?
Add your server