|
| 1 | +# Authentication Component |
| 2 | + |
| 3 | +You can use the `AuthenticationComponent` to access the result of |
| 4 | +authentication, get user identity and logout user. Load the component in your |
| 5 | +`AppController::initialize()` like any other component: |
| 6 | + |
| 7 | +``` php |
| 8 | +$this->loadComponent('Authentication.Authentication', [ |
| 9 | + 'logoutRedirect' => '/users/login' // Default is false |
| 10 | +]); |
| 11 | +``` |
| 12 | + |
| 13 | +Once loaded, the `AuthenticationComponent` will require that all actions have an |
| 14 | +authenticated user present, but perform no other access control checks. You can |
| 15 | +disable this check for specific actions using `allowUnauthenticated()`: |
| 16 | + |
| 17 | +``` php |
| 18 | +// In your controller's beforeFilter method. |
| 19 | +$this->Authentication->allowUnauthenticated(['view']); |
| 20 | +``` |
| 21 | + |
| 22 | +## Accessing the logged in user |
| 23 | + |
| 24 | +You can get the authenticated user identity data using the authentication |
| 25 | +component: |
| 26 | + |
| 27 | +``` php |
| 28 | +$user = $this->Authentication->getIdentity(); |
| 29 | +``` |
| 30 | + |
| 31 | +You can also get the identity directly from the request instance: |
| 32 | + |
| 33 | +``` php |
| 34 | +$user = $request->getAttribute('identity'); |
| 35 | +``` |
| 36 | + |
| 37 | +## Checking the login status |
| 38 | + |
| 39 | +You can check if the authentication process was successful by accessing the |
| 40 | +result object: |
| 41 | + |
| 42 | +``` php |
| 43 | +// Using Authentication component |
| 44 | +$result = $this->Authentication->getResult(); |
| 45 | + |
| 46 | +// Using request object |
| 47 | +$result = $request->getAttribute('authentication')->getResult(); |
| 48 | + |
| 49 | +if ($result->isValid()) { |
| 50 | + $user = $request->getAttribute('identity'); |
| 51 | +} else { |
| 52 | + $this->log($result->getStatus()); |
| 53 | + $this->log($result->getErrors()); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +The result sets objects status returned from `getStatus()` will match one of |
| 58 | +these constants in the Result object: |
| 59 | + |
| 60 | +- `ResultInterface::SUCCESS`, when successful. |
| 61 | +- `ResultInterface::FAILURE_IDENTITY_NOT_FOUND`, when identity could not be found. |
| 62 | +- `ResultInterface::FAILURE_CREDENTIALS_INVALID`, when credentials are invalid. |
| 63 | +- `ResultInterface::FAILURE_CREDENTIALS_MISSING`, when credentials are missing in the request. |
| 64 | +- `ResultInterface::FAILURE_OTHER`, on any other kind of failure. |
| 65 | + |
| 66 | +The error array returned by `getErrors()` contains **additional** information |
| 67 | +coming from the specific system against which the authentication attempt was |
| 68 | +made. For example LDAP or OAuth would put errors specific to their |
| 69 | +implementation in here for easier logging and debugging the cause. But most of |
| 70 | +the included authenticators don't put anything in here. |
| 71 | + |
| 72 | +## Logging out the identity |
| 73 | + |
| 74 | +To log an identity out just do: |
| 75 | + |
| 76 | +``` php |
| 77 | +$this->Authentication->logout(); |
| 78 | +``` |
| 79 | + |
| 80 | +If you have set the `logoutRedirect` config, `Authentication::logout()` will |
| 81 | +return that value else will return `false`. It won't perform any actual redirection |
| 82 | +in either case. |
| 83 | + |
| 84 | +Alternatively, instead of the component you can also use the service to log out: |
| 85 | + |
| 86 | +``` php |
| 87 | +$return = $request->getAttribute('authentication')->clearIdentity($request, $response); |
| 88 | +``` |
| 89 | + |
| 90 | +The result returned will contain an array like this: |
| 91 | + |
| 92 | +``` php |
| 93 | +[ |
| 94 | + 'response' => object(Cake\Http\Response) { ... }, |
| 95 | + 'request' => object(Cake\Http\ServerRequest) { ... }, |
| 96 | +] |
| 97 | +``` |
| 98 | + |
| 99 | +> [!NOTE] |
| 100 | +> This will return an array containing the request and response |
| 101 | +> objects. Since both are immutable you'll get new objects back. Depending on your |
| 102 | +> context you're working in you'll have to use these instances from now on if you |
| 103 | +> want to continue to work with the modified response and request objects. |
| 104 | +
|
| 105 | +## Configure Automatic Identity Checks |
| 106 | + |
| 107 | +By default `AuthenticationComponent` will automatically enforce an identity to |
| 108 | +be present during the `Controller.startup` event. You can have this check |
| 109 | +applied during the `Controller.initialize` event instead: |
| 110 | + |
| 111 | +``` php |
| 112 | +// In your controller's initialize() method. |
| 113 | +$this->loadComponent('Authentication.Authentication', [ |
| 114 | + 'identityCheckEvent' => 'Controller.initialize', |
| 115 | +]); |
| 116 | +``` |
| 117 | + |
| 118 | +You can also disable identity checks entirely with the `requireIdentity` |
| 119 | +option or by calling `disableIdentityCheck` from the controller's `beforeFilter()` method itself: |
| 120 | + |
| 121 | +``` php |
| 122 | +$this->Authentication->disableIdentityCheck(); |
| 123 | +``` |
0 commit comments