cakephp 3 login issue

486 Views Asked by At

I have tried all tutorials , all cakephp3 document but still i am facing login problem My code is :
Appcontroller.php

public function initialize()
    {
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authorize'=> 'Controller',//added this line
            'authenticate' => [
                'Form' => [
                    'userModel' => 'Users', // Added This
                    'fields' => [
                        'username' => 'email',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login'
            ]
        ]);

        $this->Auth->allow(['add']);
    }

Userscontroller.php

public function login()
    {

        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error('Your username or password is incorrect.');
        }

    }

Model/Entity/User.php

protected $_accessible = [
        '*' => true,
        'id' => false
    ];

    // ...

    protected function _setPassword($password)
    {
        return (new DefaultPasswordHasher)->hash($password);
    }

Model/Table/UsersTable.php

class UsersTable extends Table
{

    public function validationDefault(Validator $validator)
    {
        return $validator
            ->notEmpty('username', 'A username is required')
            ->notEmpty('password', 'A password is required')
            ->notEmpty('role', 'A role is required')
            ->add('role', 'inList', [
                'rule' => ['inList', ['admin', 'author']],
                'message' => 'Please enter a valid role'
            ]);
    }

}

I have added many user in users table fields are email,username,password,role.
1. When i save the data in users table then password is saved in encrypted form
2. When i goes to login form and enter the username or password then it always display wrong user name or password
3. When i print $user = $this->Auth->identify(); it always give me empty value.
4. I cant understand what is the issue.
5. when i see sql log in debug kit , it always show ' No active database connections'
Please help me

1

There are 1 best solutions below

0
On

This is an old post, but i have the same problem and i found a solution here. Basically the size of my password column (in the database) was not large enough (just 45 characters). These was the original quote in previously link:

Hopefully your password column in the database is big enough :)