show correct error message if google login fails

This commit is contained in:
bence committed 2023-04-08 03:34:07 +02:00
1 parent 862daea29c
commit d45b790122
3 files changed
+14 -17

No files matched your search

+6 -9
View File
@@ -121,6 +121,7 @@ class LoginController
if ($redirectUrl === null) {
$redirectUrl = \Container::$routeCollection->getRoute('index')->generateLink();
}
$defaultError = 'Authentication with Google failed. Please <a href="/login/google" title="Login with Google">try again</a>!';
if ($this->request->user() !== null) {
$this->request->session()->delete('redirect_after_login');
@@ -128,7 +129,7 @@ class LoginController
}
if ($this->request->query('state') !== $this->request->session()->get('oauth_state')) {
return new HtmlContent('login/google_login');
return new HtmlContent('login/google_login_error', ['error' => $defaultError]);
}
$oAuth = new GoogleOAuth(new Request());
@@ -138,27 +139,23 @@ class LoginController
);
if (!isset($tokenData['id_token'])) {
return new HtmlContent('login/google_login');
return new HtmlContent('login/google_login_error', ['error' => $defaultError]);
}
$jwtParser = new JwtParser($tokenData['id_token']);
$idToken = $jwtParser->getPayload();
if ($idToken['nonce'] !== $this->request->session()->get('oauth_nonce')) {
return new HtmlContent('login/google_login');
return new HtmlContent('login/google_login_error', ['error' => $defaultError]);
}
if (!$idToken['email_verified']) {
return new HtmlContent('login/google_login');
return new HtmlContent('login/google_login_error', ['error' => $defaultError]);
}
$user = $this->userRepository->getByGoogleSub($idToken['sub']);
if ($user === null) {
return new JsonContent([
'error' => [
'errorText' => 'No user found for this Google account.'
]
]);
return new HtmlContent('login/google_login_error', ['error' => 'No user found for this Google account.']);
}
$this->request->setUser($user);