Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ariffehsan's avatar

tenancy for laravel - authentication on tenant redirect to central login page

hi all, i got a problem where when i use auth middleware, it redirect to central login page, not tenant login page. Im using multi-database tenant.

can somebody assist me? thanks.

0 likes
5 replies
LaryAI's avatar
Level 58

To redirect to the tenant login page instead of the central login page when using the auth middleware in Laravel, you can customize the redirectTo method in the RedirectIfAuthenticated middleware.

Here's how you can do it:

  1. Open the RedirectIfAuthenticated middleware file located at app/Http/Middleware/RedirectIfAuthenticated.php.

  2. Inside the redirectTo method, you can check if the user belongs to a tenant and redirect them to the tenant login page accordingly. You can use the Auth::user() method to retrieve the authenticated user and check if they have a tenant associated with them.

    public function redirectTo($request)
    {
        if (Auth::check() && Auth::user()->tenant) {
            return route('tenant.login'); // Replace 'tenant.login' with the actual route name for the tenant login page
        }
    
        return route('login'); // Replace 'login' with the actual route name for the central login page
    }
    
  3. Save the file.

Now, when the auth middleware is applied to a route, it will redirect the user to the tenant login page if they have a tenant associated with them. Otherwise, it will redirect them to the central login page.

Note: Make sure you have defined the appropriate routes for the tenant login page and the central login page in your routes file.

ariffehsan's avatar

if i put route to "/login", it will go to central login. not tenant.

Please or to participate in this conversation.