authenticationcakephpcakephp-bakecakephp-4.x

cakephp 4 - Authentication component not found when bake a new controller


I cannot use cake bake, I have an error relative to the authentication component. The application use this component. What I have missed ?

Here is the full log:

bin/cake bake controller styles                                         1 ↵
Baking controller class for Styles...

Creating file /opt/applications/p35/src/Controller/StylesController.php
Wrote `/opt/applications/p35/src/Controller/StylesController.php`
Bake is detecting possible fixtures...
Exception: The request object does not contain the required `authentication` attribute
In [/opt/applications/p35/vendor/cakephp/authentication/src/Controller/Component/AuthenticationComponent.php, line 141]

2020-03-13 18:20:25 Error: [Exception] The request object does not contain the required `authentication` attribute in /opt/applications/p35/vendor/cakephp/authentication/src/Controller/Component/AuthenticationComponent.php on line 141
Stack Trace:
- /opt/applications/p35/vendor/cakephp/authentication/src/Controller/Component/AuthenticationComponent.php:229
- /opt/applications/p35/src/Controller/AppController.php:82
- /opt/applications/p35/vendor/cakephp/cakephp/src/Controller/Controller.php:212
- /opt/applications/p35/vendor/cakephp/bake/src/Command/TestCommand.php:339
- /opt/applications/p35/vendor/cakephp/bake/src/Command/TestCommand.php:245
- /opt/applications/p35/vendor/cakephp/bake/src/Command/TestCommand.php:120
- /opt/applications/p35/vendor/cakephp/bake/src/Command/ControllerCommand.php:201
- /opt/applications/p35/vendor/cakephp/bake/src/Command/ControllerCommand.php:147
- /opt/applications/p35/vendor/cakephp/bake/src/Command/ControllerCommand.php:64
- /opt/applications/p35/vendor/cakephp/cakephp/src/Console/BaseCommand.php:175
- /opt/applications/p35/vendor/cakephp/cakephp/src/Console/CommandRunner.php:336
- /opt/applications/p35/vendor/cakephp/cakephp/src/Console/CommandRunner.php:171
- /opt/applications/p35/bin/cake.php:12

thanks

And the line 82 of AppController.php, in public function initialize()

76    $this->loadComponent('Authentication.Authentication',[
77              'requireIdentity'=>true,
78              'logoutRedirect' => '/users/login']);
79
80
81    $this->user=$this->Authentication->getIdentity();
82        $result = $this->Authentication->getResult();
83        if ($result->isValid()) {
84          $identity=$this->Authentication->getIdentity();
85
            ...

Solution

  • When baking controller tests, bake will create an instance of the related controller class in order to retrieve the default model for that controller, it uses that model to figure the related fixture, which is being added to the $fixtures property of the generated controller test case.

    When instantiating a controller, its initialize() method will be invoked at construction time, and that's where you problem starts. You are invoking $this->Authentication->getResult() in your controllers initialize() method, causing the authentication component to look up the required authentication data in the request object attached to the controller, and sure enough it won't find any such data, as no authentication has taken place.

    You should move the logic that invokes the authentication component into the controllers beforeFilter() callback method instead, this method will by default only run when the controller factory or the exception renderer invokes the controllers startupProcess() method, which is usually either in an actual HTTP request context, or in the context of a unit test where the possibly required authentication data can be provided.

    See also