Skip to content

Commit

Permalink
Merge pull request #295 from PrestaShopCorp/ACCOUNT-1584/fix/oauth2-l…
Browse files Browse the repository at this point in the history
…ogin-localization

[ACCOUNT-1584] Use navigator language for oauth2 login redirection
  • Loading branch information
emmanuelgautier authored Jan 31, 2023
2 parents adae9ef + 6297622 commit 5b95589
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Provider/OAuth2/Oauth2LoginTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function oauth2Login(): void

$this->setSessionReturnTo(Tools::getValue($this->getReturnToParam()));

$this->oauth2Redirect();
$this->oauth2Redirect(Tools::getValue('locale'));

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($session->has('oauth2state') && $_GET['state'] !== $session->get('oauth2state'))) {
Expand All @@ -81,14 +81,14 @@ public function oauth2Login(): void
}
}

private function oauth2Redirect(): void
private function oauth2Redirect(string $locale): void
{
$provider = $this->getProvider();

// Fetch the authorization URL from the provider; this returns the
// urlAuthorize option and generates and applies any necessary parameters
// (e.g. state).
$authorizationUrl = $provider->getAuthorizationUrl();
$authorizationUrl = $provider->getAuthorizationUrl(['ui_locales' => $locale]);

// Get the state generated for you and store it to the session.
$this->getSession()->set('oauth2state', $provider->getState());
Expand Down
3 changes: 2 additions & 1 deletion views/templates/override/controllers/login/content.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@
document.addEventListener("DOMContentLoaded", function() {
const returnTo = '{$redirect}';
const redirectUri = "{$oauthRedirectUri}";
const oauth2Uri = redirectUri + '&return_to=' + encodeURIComponent(returnTo);
const locale = (navigator.language || navigator.userLanguage || 'en').slice(0, 2);
const oauth2Uri = redirectUri + '&return_to=' + encodeURIComponent(returnTo) + '&locale=' + encodeURIComponent(locale);
document.querySelector('#ps-accounts-login').addEventListener('click', function() {
document.location = oauth2Uri;
Expand Down

0 comments on commit 5b95589

Please sign in to comment.