CakeDC / users

Users Plugin for CakePHP
https://www.cakedc.com
Other
521 stars 296 forks source link

Error setting Authentication.Password configuration #906

Closed PhantomWatson closed 3 years ago

PhantomWatson commented 3 years ago

Users plugin version: 9.0.3 CakePHP version: 4.1.4

The docs say to add the following code after loading the plugin in order to use the user's email address as their username:

$identifiers = Configure::read('Auth.Identifiers');
$identifiers['Authentication.Password']['fields']['username'] = 'email';
Configure::write('Auth.Identifiers', $identifiers);

Adding this code results in the following error for me:

Error: [InvalidArgumentException] Property  Authentication.Password.className should be defined in C:\xampp\htdocs\projects_cake4\vendor\cakedc\users\src\Loader\AuthenticationServiceLoader.php on line 110
Stack Trace:
- C:\xampp\htdocs\projects_cake4\vendor\cakedc\users\src\Loader\AuthenticationServiceLoader.php:54
- C:\xampp\htdocs\projects_cake4\vendor\cakedc\users\src\Loader\AuthenticationServiceLoader.php:37
- C:\xampp\htdocs\projects_cake4\vendor\cakedc\users\src\Plugin.php:91
- C:\xampp\htdocs\projects_cake4\vendor\cakedc\users\src\Plugin.php:56
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php:154
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php:102
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php:156
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Middleware\BodyParserMiddleware.php:159
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php:166
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php:68
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php:121
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:58
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Server.php:90
- C:\xampp\htdocs\projects_cake4\webroot\index.php:40

On a whim, I tried adding $identifiers['Authentication.Password']['className'] = 'Authentication.Password';, and that resulted in the following error:

2020-10-08 20:11:57 Error: [RuntimeException] The "Password" alias has already been loaded. The `fields` key has a value of `{"username":["username","email"],"password":"password"}` but previously had a value of `{"username":"email","password":"password"}` in C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Core\ObjectRegistry.php on line 161
Stack Trace:
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Core\ObjectRegistry.php:91
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\authentication\src\AuthenticationService.php:166
- C:\xampp\htdocs\projects_cake4\vendor\cakedc\users\src\Loader\AuthenticationServiceLoader.php:56
- C:\xampp\htdocs\projects_cake4\vendor\cakedc\users\src\Loader\AuthenticationServiceLoader.php:37
- C:\xampp\htdocs\projects_cake4\vendor\cakedc\users\src\Plugin.php:91
- C:\xampp\htdocs\projects_cake4\vendor\cakedc\users\src\Plugin.php:56
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php:154
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php:102
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php:156
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Middleware\BodyParserMiddleware.php:159
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php:166
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php:68
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php:121
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Runner.php:58
- C:\xampp\htdocs\projects_cake4\vendor\cakephp\cakephp\src\Http\Server.php:90
- C:\xampp\htdocs\projects_cake4\webroot\index.php:40

Application.php

public function bootstrap(): void
    {
        ...

        Configure::write('Users.config', ['users', 'permissions']);
        $this->addPlugin(\CakeDC\Users\Plugin::class);
        $identifiers = Configure::read('Auth.Identifiers');
        $identifiers['Authentication.Password']['fields']['username'] = 'email';
        Configure::write('Auth.Identifiers', $identifiers);
    }
rochamarcelo commented 3 years ago

@PhantomWatson The doc is wrong please try:

$identifiers = Configure::read('Auth.Identifiers');
$identifiers['Password']['fields']['username'] = 'email';
Configure::write('Auth.Identifiers', $identifiers);
PhantomWatson commented 3 years ago

That indeed solves the problem. Thanks!

I suppose this issue should stay open until the documentation is updated.

rochamarcelo commented 3 years ago

Yes. Thank you for reporting.

steinkel commented 3 years ago

Fixed documentation, thank you!