diff --git a/CHANGELOG.md b/CHANGELOG.md index 253a3bf..54daf36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. +## [1.1.3] - 2026-09-09 + +### Fixed + +- Use Inertia's location response for tenant-less SPA visits so Nova navigates to the selector instead of displaying its unrecoverable-error page. +- Return tenant-selection responses directly from middleware so Nova's exception handler cannot replace them with its generic error page. + ## [1.1.2] - 2026-09-09 ### Changed @@ -53,6 +60,7 @@ All notable changes to this project will be documented in this file. - Optional tenant-aware Nova resource base and reusable context helpers. - English and Persian translations. +[1.1.3]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.1.3 [1.1.2]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.1.2 [1.1.0]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.1.0 [1.0.2]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.0.2 diff --git a/src/Exceptions/TenantNotSelected.php b/src/Exceptions/TenantNotSelected.php index 7753671..6eb9a82 100644 --- a/src/Exceptions/TenantNotSelected.php +++ b/src/Exceptions/TenantNotSelected.php @@ -20,7 +20,7 @@ class TenantNotSelected extends RuntimeException implements Responsable $destination = $this->selectorUrl(); if ($request instanceof Request && $request->header('X-Inertia') === 'true') { - return redirect()->to($destination); + return response('', 409, ['X-Inertia-Location' => url($destination)]); } if ($request instanceof Request && $request->expectsJson()) { diff --git a/src/Http/Middleware/RequireTenant.php b/src/Http/Middleware/RequireTenant.php index 5b5b942..44d93c7 100644 --- a/src/Http/Middleware/RequireTenant.php +++ b/src/Http/Middleware/RequireTenant.php @@ -5,6 +5,7 @@ namespace MeghdadFadaee\NovaTenancy\Http\Middleware; use Closure; use Illuminate\Http\Request; use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext; +use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected; use Symfony\Component\HttpFoundation\Response; class RequireTenant @@ -14,7 +15,9 @@ class RequireTenant /** @param Closure(Request): Response $next */ public function handle(Request $request, Closure $next): Response { - $this->currentTenant->requireModel(); + if (! $this->currentTenant->hasTenant()) { + return (new TenantNotSelected)->toResponse($request); + } return $next($request); } diff --git a/tests/Feature/CurrentTenantTest.php b/tests/Feature/CurrentTenantTest.php index d5c1934..b249b6f 100644 --- a/tests/Feature/CurrentTenantTest.php +++ b/tests/Feature/CurrentTenantTest.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext; use MeghdadFadaee\NovaTenancy\CurrentTenant; use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected; +use MeghdadFadaee\NovaTenancy\Http\Middleware\RequireTenant; use MeghdadFadaee\NovaTenancy\NovaTenancy; use MeghdadFadaee\NovaTenancy\Providers\UserTenantProvider; use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant; @@ -46,7 +47,7 @@ it('provides scoped string bindings and strict accessors', function (): void { ->toThrow(TenantNotSelected::class); }); -it('redirects an inertia page request to tenant selection', function (): void { +it('sends an inertia location response to tenant selection', function (): void { $request = Request::create('/nova/dashboards/home', server: [ 'HTTP_ACCEPT' => 'text/html, application/xhtml+xml', 'HTTP_X_INERTIA' => 'true', @@ -55,8 +56,22 @@ it('redirects an inertia page request to tenant selection', function (): void { $response = (new TenantNotSelected)->toResponse($request); - expect($response->getStatusCode())->toBe(302) - ->and($response->headers->get('Location'))->toEndWith('/nova/nova-tenancy'); + expect($response->getStatusCode())->toBe(409) + ->and($response->headers->get('X-Inertia-Location'))->toEndWith('/nova/nova-tenancy'); +}); + +it('returns the selection response directly from tenant middleware', function (): void { + $request = Request::create('/nova/dashboards/home', server: [ + 'HTTP_ACCEPT' => 'text/html, application/xhtml+xml', + 'HTTP_X_INERTIA' => 'true', + 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest', + ]); + + $response = (new RequireTenant(app(CurrentTenantContext::class))) + ->handle($request, fn () => response()->noContent()); + + expect($response->getStatusCode())->toBe(409) + ->and($response->headers->get('X-Inertia-Location'))->toEndWith('/nova/nova-tenancy'); }); it('returns a conflict response for a json api request', function (): void {