Skip to content

Commit 32eb7e7

Browse files
authored
Merge pull request #503 from kenjis/fix-pending-registration-magic-link-error
fix: Error exception occurs when user with pending activation tries magic-link login
2 parents dd18be0 + 924ec54 commit 32eb7e7

File tree

11 files changed

+93
-4
lines changed

11 files changed

+93
-4
lines changed

src/Authentication/Authenticators/Session.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,28 @@ private function checkUserState(): void
407407

408408
/**
409409
* Has Auth Action?
410+
*
411+
* @param int|string|null $userId Provide user id only when checking a
412+
* not-logged-in user
413+
* (e.g. user who tries magic-link login)
410414
*/
411-
public function hasAction(): bool
415+
public function hasAction($userId = null): bool
412416
{
417+
// Check not-logged-in user
418+
if ($userId !== null) {
419+
$user = $this->provider->findById($userId);
420+
421+
// Check identities for actions
422+
if ($this->getIdentitiesForAction($user) !== []) {
423+
// Make pending login state
424+
$this->user = $user;
425+
$this->setSessionKey('id', $user->id);
426+
$this->setAuthAction();
427+
428+
return true;
429+
}
430+
}
431+
413432
// Check the Session
414433
if ($this->getSessionKey('auth_action')) {
415434
return true;
@@ -461,10 +480,10 @@ private function setAuthAction(): bool
461480
*
462481
* @return UserIdentity[]
463482
*/
464-
private function getIdentitiesForAction(): array
483+
private function getIdentitiesForAction(User $user): array
465484
{
466485
return $this->userIdentityModel->getIdentitiesByTypes(
467-
$this->user,
486+
$user,
468487
$this->getActionTypes()
469488
);
470489
}
@@ -693,7 +712,7 @@ public function login(User $user): void
693712
$this->user = $user;
694713

695714
// Check identities for actions
696-
if ($this->getIdentitiesForAction() !== []) {
715+
if ($this->getIdentitiesForAction($user) !== []) {
697716
throw new LogicException(
698717
'The user has identities for action, so cannot complete login.'
699718
. ' If you want to start to login with auth action, use startLogin() instead.'

src/Controllers/MagicLinkController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ public function verify(): RedirectResponse
164164
/** @var Session $authenticator */
165165
$authenticator = auth('session')->getAuthenticator();
166166

167+
// If an action has been defined
168+
if ($authenticator->hasAction($identity->user_id)) {
169+
return redirect()->route('auth-action-show')->with('error', lang('Auth.needActivate'));
170+
}
171+
167172
// Log the user in
168173
$authenticator->loginById($identity->user_id);
169174

src/Language/de/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
'emailActivateSubject' => 'Ihr Aktivierungscode',
8888
'emailActivateMailBody' => 'Bitte verwenden Sie den unten stehenden Code, um Ihr Konto zu aktivieren und die Website zu nutzen.',
8989
'invalidActivateToken' => 'Der Code war falsch.',
90+
'needActivate' => '(To be translated) You must complete your registration by confirming the code sent to your email address.',
9091

9192
// Groups
9293
'unknownGroup' => '{0} ist eine ungültige Gruppe.',

src/Language/en/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
'emailActivateSubject' => 'Your activation code',
8888
'emailActivateMailBody' => 'Please use the code below to activate your account and start using the site.',
8989
'invalidActivateToken' => 'The code was incorrect.',
90+
'needActivate' => 'You must complete your registration by confirming the code sent to your email address.',
9091

9192
// Groups
9293
'unknownGroup' => '{0} is not a valid group.',

src/Language/es/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
'emailActivateSubject' => 'Tu código de activación',
8888
'emailActivateMailBody' => 'Por favor, usa el código de abajo para activar tu cuenta y empezar a usar el sitio.',
8989
'invalidActivateToken' => 'El código no es correcto.',
90+
'needActivate' => '(To be translated) You must complete your registration by confirming the code sent to your email address.',
9091

9192
// Grupos
9293
'unknownGroup' => '{0} no es un grupo válido.',

src/Language/fa/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
'emailActivateSubject' => 'کد فعالسازی شما',
9797
'emailActivateMailBody' => 'لطفا برای فعالسازی حساب کاربری و استفاده از سایت از کد زیر استفاده کنید.',
9898
'invalidActivateToken' => 'کد صحیح نمی باشد.',
99+
'needActivate' => 'شما باید با ارائه کد ارسال شده به ایمیلتان، ثبت نام را تکمیل کنید.',
99100

100101
// Groups
101102
'unknownGroup' => '{0} گروهی معتبر نیست.',

src/Language/fr/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
'emailActivateSubject' => 'Votre code d\'activation',
8888
'emailActivateMailBody' => 'Veuillez utiliser le code suivant pour activer votre compte et commencer à utiliser le site.',
8989
'invalidActivateToken' => 'Le code était incorrect.',
90+
'needActivate' => 'Complétez votre inscription en confirmant le code envoyé à votre email.',
9091

9192
// Groups
9293
'unknownGroup' => '{0} n\'est pas un groupe valide.',

src/Language/id/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
'emailActivateSubject' => 'Kode aktivasi Anda',
8888
'emailActivateMailBody' => 'Silahkan gunakan kode dibawah ini untuk mengaktivasi akun Anda.',
8989
'invalidActivateToken' => 'Kode tidak sesuai.',
90+
'needActivate' => '(To be translated) You must complete your registration by confirming the code sent to your email address.',
9091

9192
// Groups
9293
'unknownGroup' => '{0} bukan grup yang sah.',

src/Language/ja/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
'emailActivateSubject' => 'アクティベーションコード', // 'Your activation code',
8888
'emailActivateMailBody' => '以下のコードを使用してアカウントを有効化し、サイトの利用を開始してください。', // 'Please use the code below to activate your account and start using the site.',
8989
'invalidActivateToken' => 'コードが間違っています。', // 'The code was incorrect.',
90+
'needActivate' => 'メールアドレスに送信されたコードを確認し、登録を完了する必要があります。', // 'You must complete your registration by confirming the code sent to your email address.',
9091

9192
// Groups
9293
'unknownGroup' => '{0} は有効なグループではありません。', // '{0} is not a valid group.',

src/Language/sk/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
'emailActivateSubject' => 'Váš aktivačný kód',
8888
'emailActivateMailBody' => 'Pomocou nižšie uvedeného kódu aktivujte svoj účet a môžete začať používať stránku.',
8989
'invalidActivateToken' => 'Kód bol nesprávny',
90+
'needActivate' => 'Registráciu musíte dokončiť potvrdením kódu zaslaného na vašu e-mailovú adresu.',
9091

9192
// Groups
9293
'unknownGroup' => '{0} nie je platná skupina.',

0 commit comments

Comments
 (0)