|
| 1 | +# Custom Authenticators |
| 2 | + |
| 3 | +CodeIgniter Shield allows you to extend authentication by creating **Custom Authenticators**. |
| 4 | +This is done by implementing the `CodeIgniter\Shield\Authentication\AuthenticatorInterface` contract, which ensures full compatibility with Shield’s authentication lifecycle, including `login` and `logout` events. |
| 5 | + |
| 6 | +Custom Authenticators enable project-specific authentication strategies such as: |
| 7 | + |
| 8 | +- External identity providers (OAuth, SAML, OpenID Connect) |
| 9 | +- Hardware or device challenges (USB Security Key, FIDO2, TPM, device fingerprinting) |
| 10 | +- Hybrid authentication mechanisms |
| 11 | + |
| 12 | +## Why Custom Authenticators |
| 13 | + |
| 14 | +While Shield provides built-in authenticators such as **Session**, **AccessTokens**, **HmacSha256**, and **JWT**, custom authenticators allow you to: |
| 15 | + |
| 16 | +- Enforce project-specific login logic |
| 17 | +- Integrate new or external authentication mechanisms |
| 18 | +- Keep full compatibility with Shield events and lifecycle |
| 19 | + |
| 20 | +## Implementing a Custom Authenticator |
| 21 | + |
| 22 | +1. Create a PHP class in your `App\Auth\Authentication` namespace. |
| 23 | +2. Implement the `CodeIgniter\Shield\Authentication\AuthenticatorInterface`. |
| 24 | +3. Implement the required methods: |
| 25 | + |
| 26 | +```php |
| 27 | +<?php |
| 28 | + |
| 29 | +declare(strict_types=1); |
| 30 | + |
| 31 | +namespace App\Auth\Authentication; |
| 32 | + |
| 33 | +use CodeIgniter\Shield\Authentication\AuthenticatorInterface; |
| 34 | +use CodeIgniter\Shield\Result; |
| 35 | +use CodeIgniter\Shield\Entities\User; |
| 36 | + |
| 37 | +class MyCustomAuthenticator implements AuthenticatorInterface |
| 38 | +{ |
| 39 | + public function attempt(array $credentials): Result |
| 40 | + { |
| 41 | + // Your login logic |
| 42 | + } |
| 43 | + |
| 44 | + public function check(array $credentials): Result |
| 45 | + { |
| 46 | + // Credential verification |
| 47 | + } |
| 48 | + |
| 49 | + public function loggedIn(): bool |
| 50 | + { |
| 51 | + // Return login state |
| 52 | + } |
| 53 | + |
| 54 | + public function login(User $user): void |
| 55 | + { |
| 56 | + // Store user session or token |
| 57 | + } |
| 58 | + |
| 59 | + public function loginById($userId): void |
| 60 | + { |
| 61 | + // Optional: login using user ID |
| 62 | + } |
| 63 | + |
| 64 | + public function logout(): void |
| 65 | + { |
| 66 | + // Remove session or token |
| 67 | + } |
| 68 | + |
| 69 | + public function getUser(): ?User |
| 70 | + { |
| 71 | + // Return the currently logged-in user |
| 72 | + } |
| 73 | + |
| 74 | + public function recordActiveDate(): void |
| 75 | + { |
| 76 | + // Optional: track user activity |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Registering the Authenticator |
| 82 | + |
| 83 | +In CodeIgniter Shield, all authenticators-built-in or custom—are registered through the **app/Config/Auth.php** file. This ensures that Shield recognizes your authenticator and allows you to reference it using its alias in the *auth* helper. |
| 84 | + |
| 85 | +Open **app/Config/Auth.php** and add your custom authenticator to the `$authenticators` array: |
| 86 | + |
| 87 | +```php |
| 88 | +public array $authenticators = [ |
| 89 | + 'session' => \CodeIgniter\Shield\Authentication\Session::class, |
| 90 | + 'tokens' => \CodeIgniter\Shield\Authentication\AccessTokens::class, |
| 91 | + 'hmac' => \CodeIgniter\Shield\Authentication\HmacSha256::class, |
| 92 | + // Register your custom authenticator |
| 93 | + 'custom' => \App\Auth\Authentication\MyCustomAuthenticator::class, |
| 94 | +]; |
| 95 | +``` |
| 96 | +The array key `custom` is the alias you will use in the `auth('custom')` helper. |
| 97 | + |
| 98 | +## Using the Authenticator |
| 99 | + |
| 100 | +You can now use your authenticator anywhere in your application via the `auth('custom')` helper: |
| 101 | + |
| 102 | +```php |
| 103 | +$credentials = [ |
| 104 | + 'email' => $this->request->getPost('email'), |
| 105 | + 'password' => $this->request->getPost('password') |
| 106 | +]; |
| 107 | + |
| 108 | +$result = auth('custom')->attempt($credentials ); |
| 109 | + |
| 110 | +if ($result->isOK()) { |
| 111 | + $user = $result->extraInfo(); |
| 112 | + echo "Login successful for: " . $user->email; |
| 113 | +} else { |
| 114 | + echo "Login failed: " . $result->reason(); |
| 115 | +} |
| 116 | + |
| 117 | +``` |
| 118 | + |
| 119 | +Now all standard authentication methods—such as `attempt()`, `check()`, `loggedIn()`, `login()`, `loginById()`, `logout()`, `getUser()`, and `recordActiveDate()`—are fully available. |
0 commit comments