Skip to content

Commit

Permalink
Adjust implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Mar 14, 2023
1 parent a3b90aa commit 3d09ed3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
21 changes: 9 additions & 12 deletions core-bundle/src/Resources/public/mootao.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ Request.Contao = new Class(
var url = this.getHeader('X-Ajax-Location'),
json;

this.redirectIfUnauthorized();

if (url && this.options.followRedirects) {
location.replace(url);
return;
Expand Down Expand Up @@ -82,23 +80,22 @@ Request.Contao = new Class(
failure: function() {
var url = this.getHeader('X-Ajax-Location');

this.redirectIfUnauthorized();
if (null === url) {
this.onFailure();
return;
}

if (url && this.options.followRedirects && this.status >= 300 && this.status < 400) {
if (401 === this.status) {
location.replace(url);
return;
}

this.onFailure();
},

redirectIfUnauthorized: function() {
var url = this.getHeader('X-Ajax-Location'),
unauthorized = null !== this.getHeader('X-Is-Unauthorized');

if (url && unauthorized) {
if (this.options.followRedirects && this.status >= 300 && this.status < 400) {
location.replace(url);
return;
}

this.onFailure();
}
});

Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/public/mootao.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function redirectToBackend(Request $request): Response
$location = $this->uriSigner->sign($url);

if ($request->isXmlHttpRequest()) {
return new Response($location, 303, ['X-Is-Unauthorized' => 'true', 'X-Ajax-Location' => $location]);
return new Response($location, 401, ['X-Ajax-Location' => $location]);
}

return new RedirectResponse($location, 302);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testRedirectsAjaxRequests(): void

$this->assertInstanceOf(Response::class, $response);
$this->assertFalse($response->headers->has('Location'));
$this->assertTrue($response->headers->has('X-Is-Unauthorized'));
$this->assertSame(401, $response->getStatusCode());
$this->assertSame('http://localhost/contao/login?_hash=%2FxSCw6cwMlws5DEhBCvs0%2F75oQA8q%2FgMkZEnYCf6QSE%3D&redirect=https%3A%2F%2Fcontao.org%2Fpreview.php%2Fabout-contao.html', $response->headers->get('X-Ajax-Location'));
}

Expand Down

0 comments on commit 3d09ed3

Please sign in to comment.