"Authentication is required to continue" when loading Authentication component

in Quick Bites


If you use cakedc/users or cakephp/authentication and want to programmatically log in a newly created user, you will have to use setIdentity() on Authentication component, and for that you will have to load it into your controller. But if you do it wrong, you will get this error:

Authentication is required to continue Authentication\Authenticator\UnauthenticatedException

To fix, you need to load the component telling it not to require the presence of any identity:

$this->loadComponent('Authentication.Authentication', ['requireIdentity' => false]);

More information and references: