88use CodeIgniter \Shield \Authentication \Authentication ;
99use CodeIgniter \Shield \Authentication \AuthenticationException ;
1010use CodeIgniter \Shield \Authentication \AuthenticatorInterface ;
11+ use CodeIgniter \Shield \Config \Auth as AuthConfig ;
1112use CodeIgniter \Shield \Entities \User ;
1213use 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 )) {
0 commit comments