Im using VueJs with Laravel 11 and Spatie multitenancy package and Sanctum:
Im sending a request but im getting the following error :
The request expected a current tenant but none was set.
Here is my code :
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__ . '/../routes/web.php',
api: __DIR__ . '/../routes/api.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware
->api(append: [
SpatieMultitenancyHttpMiddlewareNeedsTenant::class,
SpatieMultitenancyHttpMiddlewareEnsureValidTenantSession::class
]);
})
->withExceptions(function (Exceptions $exceptions) {
})->create();`
So I created a new middleware. setTenantMiddleware
:
->api(append: [
AppHttpMiddlewaresetTenantMiddleware::class,
SpatieMultitenancyHttpMiddlewareNeedsTenant::class,
SpatieMultitenancyHttpMiddlewareEnsureValidTenantSession::class
]);
now I’m getting :
Session store not set on request.
here is my setTenantMiddleware
class setTenantMiddleware
{
public function handle($request, Closure $next)
{
// Get the tenant based on the request's host (or other identifier)
$host = $request->getHost(); // You can adjust this logic as needed
$tenant = Tenant::where('domain', 'tenant1.localhost')->first();
if ($tenant) {
// Set the tenant as the current tenant
$tenant->makeCurrent();
Log::info('Tenant context switched for domain: ' . $host . ', Tenant ID: ' . $tenant->id);
} else {
Log::error('Tenant not found for domain: ' . $host);
abort(404, 'Tenant not found.');
}
// Proceed with the request
return $next($request);
}
}