show correct error message if google login fails

This commit is contained in:
Bence Pőcze 2023-04-08 03:34:07 +02:00
parent 862daea29c
commit d45b790122
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D
3 changed files with 14 additions and 17 deletions

View File

@ -121,6 +121,7 @@ class LoginController
if ($redirectUrl === null) { if ($redirectUrl === null) {
$redirectUrl = \Container::$routeCollection->getRoute('index')->generateLink(); $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) { if ($this->request->user() !== null) {
$this->request->session()->delete('redirect_after_login'); $this->request->session()->delete('redirect_after_login');
@ -128,7 +129,7 @@ class LoginController
} }
if ($this->request->query('state') !== $this->request->session()->get('oauth_state')) { 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()); $oAuth = new GoogleOAuth(new Request());
@ -138,27 +139,23 @@ class LoginController
); );
if (!isset($tokenData['id_token'])) { 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']); $jwtParser = new JwtParser($tokenData['id_token']);
$idToken = $jwtParser->getPayload(); $idToken = $jwtParser->getPayload();
if ($idToken['nonce'] !== $this->request->session()->get('oauth_nonce')) { 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']) { 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']); $user = $this->userRepository->getByGoogleSub($idToken['sub']);
if ($user === null) { if ($user === null) {
return new JsonContent([ return new HtmlContent('login/google_login_error', ['error' => 'No user found for this Google account.']);
'error' => [
'errorText' => 'No user found for this Google account.'
]
]);
} }
$this->request->setUser($user); $this->request->setUser($user);

View File

@ -1,8 +0,0 @@
@extends(templates/layout_normal)
@section(main)
<h2>Login up with Google</h2>
<div class="box">
<p class="error justify">Authentication with Google failed. Please <a href="/login/google" title="Login with Google">try again</a>!</p>
</div>
@endsection

View File

@ -0,0 +1,8 @@
@extends(templates/layout_normal)
@section(main)
<h2>Login up with Google</h2>
<div class="box">
<p class="error justify"><?= $error ?></p>
</div>
@endsection