The logic is way too complex for me. But I think your middleware may need fixing.
if auth()->check() fails, should you not throw an error or redirect to login page?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi everyone, I am creating SaaS project and trying to use filament for my admin panel. As you know, SaaS project has a single code base with multiple databases. The whole project is written as a single database, there is no customization in any of my models.
My solution should be something like this
I create a MASTER database that is default and create a companies table that puts all customers. With the login request, I switch all databases by companies table and find the user to which company it belongs and switch to this database finally, create the token with database name in payload and go!
Another middleware to check the valid token and also to switch in the corresponding database and go!
But this solutions doesn't work.
My Login customization
public function authenticate(): ?\Filament\Http\Responses\Auth\Contracts\LoginResponse
{
$databases = DB::table('companies')->get();
$credentials = $this->getCredentialsFromFormData($this->form->getState());
$userService = new UserService();
foreach ($databases as $database) {
Config::set('database.connections.mysql.database', $database->database);
DB::reconnect('mysql');
$user_found = $userService->getUserByEmail($credentials['email']);
if ($user_found) {
$response = parent::authenticate();
Session::put('company', $database);
return $response;
}
}
Filament::auth()->logout();
$this->throwFailureValidationException();
}
My middleware
public function handle(Request $request, Closure $next)
{
if(auth()->check()) {
$company = session('company');
if ($company) {
Config::set('database.connections.mysql.database', $company->database);
DB::reconnect('mysql');
return $next($request);
}
return redirect('/');
}
return $next($request);
}
When user isn't authenticated, automatically redirected to the dashboard with empty values. I don't have an idea why is happend
Please or to participate in this conversation.