It looks like you're having trouble with Stancl Tenancy not switching to the tenant database when using Fortify for authentication. This is a common issue when dealing with multi-tenancy and authentication systems. Here are a few steps to ensure that the tenant database is correctly switched:
-
Ensure Middleware Order: The order of middleware is crucial. Make sure that the tenancy initialization middleware (
InitializeTenancyByDomain) is executed before any other middleware that might interact with the database. -
Check Configuration: Ensure that your
config/database.phpis correctly set up to handle tenant connections. Thetenancypackage should be configured to switch databases based on the tenant. -
Custom Middleware: Sometimes, you might need to create custom middleware to ensure that the tenant context is correctly set before any Fortify actions are taken.
Here's a step-by-step solution:
Step 1: Ensure Middleware Order
Make sure your tenant.php routes file has the correct middleware order:
Route::middleware([
'web',
InitializeTenancyByDomain::class,
PreventAccessFromCentralDomains::class,
])->group(function () {
Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
'verified',
])->group(function () {
Route::get('/dashboard', function () {
return Inertia::render('Dashboard');
})->name('dashboard');
Route::get('/', function () {
return Inertia::render('Dashboard');
})->name('dashboard');
});
Route::group(['middleware' => config('fortify.middleware', ['web'])], function () {
$enableViews = config('fortify.views', true);
if ($enableViews) {
Route::get(RoutePath::for('login', '/login'), [AuthenticatedSessionController::class, 'create'])
->middleware(['guest:' . config('fortify.guard')])
->name('login');
}
$limiter = config('fortify.limiters.login');
$twoFactorLimiter = config('fortify.limiters.two-factor');
$verificationLimiter = config('fortify.limiters.verification', '6,1');
Route::post(RoutePath::for('login', '/login'), [AuthenticatedSessionController::class, 'store'])
->middleware(array_filter([
'guest:' . config('fortify.guard'),
$limiter ? 'throttle:' . $limiter : null,
]));
});
});
Step 2: Check Database Configuration
Ensure your config/database.php is set up to handle tenant connections. You should have something like this:
'connections' => [
'tenant' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => null, // This will be set dynamically
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
Step 3: Custom Middleware (if needed)
If the above steps do not resolve the issue, you might need to create custom middleware to ensure the tenant context is set before Fortify actions. Here’s an example of custom middleware:
namespace App\Http\Middleware;
use Closure;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
class EnsureTenantDatabase
{
public function handle($request, Closure $next)
{
// Initialize tenancy
app(InitializeTenancyByDomain::class)->handle($request, function () {});
return $next($request);
}
}
Register this middleware in your app/Http/Kernel.php:
protected $routeMiddleware = [
// Other middleware
'ensure.tenant.database' => \App\Http\Middleware\EnsureTenantDatabase::class,
];
Then, use this middleware in your routes:
Route::middleware([
'web',
'ensure.tenant.database',
])->group(function () {
// Your routes here
});
By following these steps, you should be able to ensure that the tenant database is correctly switched when using Fortify for authentication.