Skip to content

Commit 5ac8a8f

Browse files
committed
Restore request and session
1 parent af75718 commit 5ac8a8f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/ResponseFactory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,28 @@ public function render(string $component, $props = []): Response
107107
*/
108108
public function location($url): \Symfony\Component\HttpFoundation\Response
109109
{
110+
$request = $session = null;
111+
110112
if ($url instanceof RedirectResponse) {
113+
$request = $url->getRequest();
114+
115+
$session = $url->getSession();
116+
111117
$url = $url->getTargetUrl();
112118
}
113119

114120
if (Request::inertia()) {
115121
return BaseResponse::make('', 409, ['X-Inertia-Location' => $url]);
116122
}
117123

118-
return new RedirectResponse($url);
124+
return tap(new RedirectResponse($url), function ($redirect) use ($request, $session) {
125+
if ($request) {
126+
$redirect->setRequest($request);
127+
}
128+
129+
if ($session) {
130+
$redirect->setSession($session);
131+
}
132+
});
119133
}
120134
}

tests/ResponseFactoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
use Illuminate\Support\Facades\Request;
1212
use Inertia\Tests\Stubs\ExampleMiddleware;
1313
use Illuminate\Contracts\Support\Arrayable;
14+
use Illuminate\Http\Request as HttpRequest;
1415
use Illuminate\Session\Middleware\StartSession;
16+
use Illuminate\Session\NullSessionHandler;
17+
use Illuminate\Session\Store;
1518

1619
class ResponseFactoryTest extends TestCase
1720
{
@@ -75,6 +78,20 @@ public function test_location_response_for_non_inertia_requests_using_redirect_r
7578
$this->assertEquals('https://inertiajs.com', $response->headers->get('location'));
7679
}
7780

81+
public function test_location_response_for_non_inertia_requests_using_redirect_response_with_existing_session_and_request_properties(): void
82+
{
83+
$redirect = new RedirectResponse('https://inertiajs.com');
84+
$redirect->setSession($session = new Store('test', new NullSessionHandler));
85+
$redirect->setRequest($request = new HttpRequest);
86+
$response = (new ResponseFactory())->location($redirect);
87+
88+
$this->assertInstanceOf(RedirectResponse::class, $response);
89+
$this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
90+
$this->assertEquals('https://inertiajs.com', $response->headers->get('location'));
91+
$this->assertSame($session, $response->getSession());
92+
$this->assertSame($request, $response->getRequest());
93+
}
94+
7895
public function test_the_version_can_be_a_closure(): void
7996
{
8097
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {

0 commit comments

Comments
 (0)