Skip to content

Commit 97bda91

Browse files
committed
refactor!: defer instantiation of Authentication
1 parent 543a36a commit 97bda91

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/Auth.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodeIgniter\Shield\Authentication\Authentication;
99
use CodeIgniter\Shield\Authentication\AuthenticationException;
1010
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
11+
use CodeIgniter\Shield\Config\Auth as AuthConfig;
1112
use CodeIgniter\Shield\Entities\User;
1213
use CodeIgniter\Shield\Models\UserModel;
1314

@@ -32,7 +33,7 @@ class Auth
3233
*/
3334
public const SHIELD_VERSION = '1.0.0-beta.7';
3435

35-
protected Authentication $authenticate;
36+
protected ?Authentication $authenticate = null;
3637

3738
/**
3839
* The Authenticator alias to use for this request.
@@ -41,9 +42,19 @@ class Auth
4142

4243
protected ?UserModel $userProvider = null;
4344

44-
public function __construct(Authentication $authenticate)
45+
protected function ensureAuthentication(): void
4546
{
46-
$this->authenticate = $authenticate->setProvider($this->getProvider());
47+
if ($this->authenticate !== null) {
48+
return;
49+
}
50+
51+
/** @var AuthConfig $config */
52+
$config = config('Auth');
53+
54+
$authenticate = new Authentication($config);
55+
$authenticate->setProvider($this->getProvider());
56+
57+
$this->authenticate = $authenticate;
4758
}
4859

4960
/**
@@ -65,6 +76,8 @@ public function setAuthenticator(?string $alias = null): self
6576
*/
6677
public function getAuthenticator(): AuthenticatorInterface
6778
{
79+
$this->ensureAuthentication();
80+
6881
return $this->authenticate
6982
->factory($this->alias);
7083
}
@@ -93,6 +106,8 @@ public function id()
93106

94107
public function authenticate(array $credentials): Result
95108
{
109+
$this->ensureAuthentication();
110+
96111
return $this->authenticate
97112
->factory($this->alias)
98113
->attempt($credentials);
@@ -160,6 +175,8 @@ public function getProvider(): UserModel
160175
*/
161176
public function __call(string $method, array $args)
162177
{
178+
$this->ensureAuthentication();
179+
163180
$authenticator = $this->authenticate->factory($this->alias);
164181

165182
if (method_exists($authenticator, $method)) {

src/Config/Services.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use CodeIgniter\Config\BaseService;
88
use CodeIgniter\Shield\Auth;
9-
use CodeIgniter\Shield\Authentication\Authentication;
109
use CodeIgniter\Shield\Authentication\JWTManager;
1110
use CodeIgniter\Shield\Authentication\Passwords;
1211

@@ -21,9 +20,7 @@ public static function auth(bool $getShared = true): Auth
2120
return self::getSharedInstance('auth');
2221
}
2322

24-
$config = config('Auth');
25-
26-
return new Auth(new Authentication($config));
23+
return new Auth();
2724
}
2825

2926
/**

0 commit comments

Comments
 (0)