1
votes

I'm playing around with CakePHP and can't seem to get the login working. It seems that $this->Auth->identify() is constantly returning false and not allowing me to login.

I've read all previous posts regarding my issue, however, none have provided me with a solution. The users I am trying to log in as have all been created using the cake password hasher, which I have checked have been stored in the database. I have checked the password field length in the database, which is set to varchar (255), I've checked the Auth => authenticate => Form => fields are set to the correct values (the login.ctp fields are also correct). I also tried changing the $this->Form->control() to $this->Form->input() as someone suggested, with no luck.

AppController:

 $this->loadComponent('Auth', [
            'loginRedirect' => [
                'controller' => 'Users',
                'action' => 'classes'
            ],
            'logoutRedirect' => [
                'controller' => 'Users',
                'action' => 'index'
            ]
  ]);
$this->loadComponent('Auth', [
                'authenticate' => [
                    'Form' => [
                        'fields' => [
                            'username' => 'email',
                            'password' => 'password'
                        ]
                    ]
                ],
                'loginAction' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ]
        ]);

login() function in UsersController:

public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            pj($user);
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect(['controller' => 'users']);
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }

login.ctp:

<div class="users form">
<?= $this->Form->create() ?>
    <fieldset>
        <legend><?= __('Please enter your username and password') ?></legend>
        <?= $this->Form->input('email') ?>
        <?= $this->Form->input('password') ?>
    </fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>

EDIT: I forgot to add that I can successfully add users, I just can't log them in.

1
Debug the sourcecode to figure out where exactly the authentication flow bails out, that should give you an idea what might be the problem.ndm
what is the length of your PASSWORD column in the database?Oerd
@Oerd my password field length is 255Bl3akMirai

1 Answers

0
votes

You are loading the AuthComponent twice in AppController. The second load with the config for form fields is not loaded.

Load the component one time with the wanted config.

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'fields' => [
                'username' => 'email',
                'password' => 'password'
            ]
        ]
    ],
    'loginRedirect' => [
        'controller' => 'Users',
        'action' => 'classes'
    ],
    'logoutRedirect' => [
        'controller' => 'Users',
        'action' => 'index'
    ],
    'loginAction' => [
        'controller' => 'Users',
        'action' => 'login'
    ]
]);