DEV Community

Cover image for A Laravel Package for Post-Authentication Multi-Tenancy with Automatic Tenant Switching
Chief Technical Officer
Chief Technical Officer

Posted on

A Laravel Package for Post-Authentication Multi-Tenancy with Automatic Tenant Switching

Multi-tenancy in Laravel is not new, but many implementations assume tenancy is resolved before authentication. That works well for some architectures, especially subdomain-first systems, but it does not fit every application.

In many real-world SaaS platforms, the tenant context is determined after a user logs in or registers. The application needs to inspect the authenticated user, determine which tenant they belong to, and then switch the database connection or tenant context automatically. That is the problem I wanted to solve.

So we built a Laravel package focused on post-authentication multi-tenancy with automatic tenant detection, database switching, middleware support, model scoping, and tenant management tooling.

Why we Built It

We wanted a package that could integrate into a Laravel application that already had authentication in place and then handle tenancy cleanly from there.

The goals were straightforward:

  • detect the tenant after login
  • switch tenant context automatically
  • support isolated tenant databases
  • work with common Laravel authentication stacks
  • reduce the amount of custom middleware, listeners, and connection logic developers have to write by hand

Instead of forcing a one-size-fits-all architecture, I wanted something that could work with different application flows and different database engines.

What the Package Does

This package provides post-authentication multi-tenancy for Laravel applications.

Once a user authenticates, the package can automatically determine the correct tenant and switch the application into that tenant context. It also includes support for:

  • automatic tenant switching through middleware
  • multiple tenant database connections
  • model traits for tenant-aware queries
  • artisan commands for installation, creation, status, migration, seeding, and cleanup
  • support for MySQL, PostgreSQL, SQLite, and SQL Server
  • optional automatic tenant creation and event-driven setup flows
  • security validation and tenant access checks

The result is a package designed to make tenant-aware Laravel applications easier to implement and easier to maintain.

What Makes This Different

What makes this package different is its post-authentication focus.

A lot of tenancy discussions in Laravel revolve around identifying the tenant from a domain, subdomain, or request context before the user is authenticated. That is valid, but not every system works that way.

Some systems need to determine the tenant by looking at the authenticated user first. Others need a fallback strategy. Others need support for multiple tenant-detection methods without rewriting core application logic.

This package was built for that reality.

It supports multiple detection strategies, middleware-driven switching, multiple database drivers, and a workflow that fits applications where authentication already exists and tenancy needs to be layered in cleanly afterward.

Installation

Getting started is intentionally simple:

composer require worldesports/laravel-auto-tenancy
php artisan tenant:install --force --migrate
Enter fullscreen mode Exit fullscreen mode

Question

If you have any questions you can either reply directly to this post or email me at chieftech@worldesports.app

Top comments (1)