1

Is it possible to configure CakeDC's Users plugin to use email field as a username?

By default there are both username and email fields in Users plugin and all work great! But I would like to use email as the username for the authentication, so the user registration can be simplified.

I have tried overriding the UsersAuthComponent by loading Auth component in the AppController, but login stops working and says "Wrong username or password".

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginAction' => [
            'plugin' => 'CakeDC/Users',
            'controller' => 'Users',
            'action' => 'login',
        ],
        'authenticate' => [
            'all' => [
                'scope' => ['active' => 1]
            ],
            'CakeDC/Users.RememberMe',
            'Form' => [
                'fields' => ['username' => 'email', 'password' => 'password']
            ]
        ],
        'authorize' => [
            'CakeDC/Users.Superuser',
            'CakeDC/Users.SimpleRbac',
        ],
        'storage' => 'Session'
    ]);

    $this->loadComponent('CakeDC/Users.UsersAuth');
}

Following the documentation, I also turned off the UsersAuthComponent in bootstrap.php.

Configure::write('Users.auth', false);

Is there a trick? I believe it is possible, or maybe a bug?

I am using CakePHP 3.1 and CakeDC User plugin 3.1

2 Answers2

1

The piece your are possibly missing is to override the login.ctp template to change the form and let the user enter his email.

To do so you need to create a file under: /src/Template/Plugin/CakeDC/Users/Users/login.ctp with the content you would like to have for your login page (usually you'll want to override this page in every app you do) and ensure you create an input "email" in that view. The code could be like this

<div class="users form">
    <?= $this->Flash->render('auth') ?>
    <?= $this->Form->create() ?>
    <fieldset>
        <legend><?= __d('Users', 'Please enter your username and password') ?></legend>
        <?= $this->Form->input('email', ['required' => true]) ?>
        <?= $this->Form->input('password', ['required' => true]) ?>
    </fieldset>
    <?= $this->Form->button(__d('Users', 'Login')); ?>
    <?= $this->Form->end() ?>
</div>

You could use the CakeDC/Users login.ctp template as an example if you want to keep the Social Login links, remember me, etc. I'm using a simplified version of the login form as an example here.

We'll be improving the plugin soon to simplify this override.

steinkel
  • 1,156
  • 9
  • 15
  • We've just updated the docs https://github.com/CakeDC/users/blob/master/Docs/Documentation/Configuration.md#using-the-users-email-to-login to point others in the same situation – steinkel Feb 19 '16 at 01:40
0

I think this is what MultiColumnAuthenticate is used for, just viewing the plugin on GitHub. Try something like this:

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginAction' => [
            'plugin' => 'CakeDC/Users',
            'controller' => 'Users',
            'action' => 'login',
        ],
        'authenticate' => [
            'all' => [
                'scope' => ['active' => 1]
            ],
            'CakeDC/Users.RememberMe',
            'FOC/Authenticate.MultiColumn' => [
                'fields' => [
                    'username' => 'username',
                    'password' => 'password'
                ],
                'columns' => ['username', 'email'],
                'userModel' => 'Users',
            ]
        ],
        'authorize' => [
            'CakeDC/Users.Superuser',
            'CakeDC/Users.SimpleRbac',
        ],
        'storage' => 'Session'
    ]);

    $this->loadComponent('CakeDC/Users.UsersAuth');
}

This would check against both email and username when logging in.

Isaac Askew
  • 1,281
  • 2
  • 17
  • 30
  • Hi Isaac! thanks for your answer...unfortunately I was not having time to test it until now. I was looking for the "**FOC/Authenticate.MultiColumn**" plugin, but the site says is not maintained anymore. But it helped to navigate me there to get the information I needed ;-) FOC were saying on github: "**MultiColumnAuthenticate, this can now be achieved with custom finders in CakePHP 3**" So I realised that it must be done in clean way with cake 3.1 !! **and it worked.** – user3135308 Jan 16 '16 at 15:07
  • The mistake was, that I kept sending wrong input name from my template: `$this->Form->input('username');` instead of `$this->Form->input('email');` So even if it was configured good,it was not getting the email field configured! I just assumed, that CakeDC plugin will render the field for me, but it did not, the .ctp template must be customized. – user3135308 Jan 16 '16 at 15:18
  • I confirm, that overriding Auth as mentioned in main post above: `...'Form' => [ 'fields' => ['username' => 'email', 'password' => 'password'] ]...` in your customized config **users.php** will do the trick only with CakePhp3.1 + CakeDC Users Plugin. Well documented also here: [link](http://book.cakephp.org/3.0/en/controllers/components/authentication.html) (look for "fields") – user3135308 Jan 16 '16 at 15:21