Skip to content

Commit 3f365a7

Browse files
committed
refactor: move createIdentityEmailActivate() in Session to Actions/EmailActivator
1 parent eef22a0 commit 3f365a7

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

src/Authentication/Actions/EmailActivator.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use CodeIgniter\Shield\Entities\User;
1010
use CodeIgniter\Shield\Exceptions\LogicException;
1111
use CodeIgniter\Shield\Exceptions\RuntimeException;
12+
use CodeIgniter\Shield\Models\UserIdentityModel;
1213

1314
class EmailActivator implements ActionInterface
1415
{
@@ -34,7 +35,7 @@ public function show(): string
3435
);
3536
}
3637

37-
$code = $authenticator->createIdentityEmailActivate();
38+
$code = $this->createIdentity($user);
3839

3940
// Send the email
4041
helper('email');
@@ -88,4 +89,40 @@ public function verify(IncomingRequest $request)
8889
// Get our login redirect url
8990
return redirect()->to(config('Auth')->loginRedirect());
9091
}
92+
93+
/**
94+
* Called from `RegisterController::registerAction()`
95+
*/
96+
public function afterRegister(User $user): void
97+
{
98+
$this->createIdentity($user);
99+
}
100+
101+
/**
102+
* Create an identity for Email Activation
103+
*
104+
* @return string The secret code
105+
*/
106+
private function createIdentity(User $user): string
107+
{
108+
helper('text');
109+
110+
/** @var UserIdentityModel $userIdentityModel */
111+
$userIdentityModel = model(UserIdentityModel::class);
112+
113+
$userIdentityModel->deleteIdentitiesByType($user->getAuthId(), 'email_activate');
114+
115+
// Create an identity for our activation hash
116+
$code = random_string('nozero', 6);
117+
118+
$userIdentityModel->insert([
119+
'user_id' => $user->getAuthId(),
120+
'type' => 'email_activate',
121+
'secret' => $code,
122+
'name' => 'register',
123+
'extra' => lang('Auth.needVerification'),
124+
]);
125+
126+
return $code;
127+
}
91128
}

src/Authentication/Authenticators/Session.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -653,29 +653,4 @@ private function refreshRememberMeToken(stdClass $token)
653653

654654
$this->setRememberMeCookie($rawToken);
655655
}
656-
657-
/**
658-
* Create an identity for Email Activation
659-
*
660-
* @return string The secret code
661-
*/
662-
public function createIdentityEmailActivate(): string
663-
{
664-
helper('text');
665-
666-
$this->userIdentityModel->deleteIdentitiesByType($this->user->getAuthId(), 'email_activate');
667-
668-
// Create an identity for our activation hash
669-
$code = random_string('nozero', 6);
670-
671-
$this->userIdentityModel->insert([
672-
'user_id' => $this->user->getAuthId(),
673-
'type' => 'email_activate',
674-
'secret' => $code,
675-
'name' => 'register',
676-
'extra' => lang('Auth.needVerification'),
677-
]);
678-
679-
return $code;
680-
}
681656
}

src/Controllers/RegisterController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodeIgniter\Shield\Controllers;
44

55
use App\Controllers\BaseController;
6+
use CodeIgniter\Config\Factories;
67
use CodeIgniter\Events\Events;
78
use CodeIgniter\HTTP\RedirectResponse;
89
use CodeIgniter\Shield\Authentication\Authenticators\Session;
@@ -90,9 +91,13 @@ public function registerAction(): RedirectResponse
9091
$actionClass = setting('Auth.actions')['register'] ?? null;
9192

9293
if (! empty($actionClass)) {
93-
session()->set('auth_action', $actionClass);
94+
$action = Factories::actions($actionClass); // @phpstan-ignore-line
95+
96+
if (method_exists($action, 'afterRegister')) {
97+
$action->afterRegister($user);
98+
}
9499

95-
$authenticator->createIdentityEmailActivate();
100+
session()->set('auth_action', $actionClass);
96101

97102
return redirect()->to('auth/a/show');
98103
}

0 commit comments

Comments
 (0)