2
votes

I am trying to log in a user on my Cakephp 3.0 site. My table has a 'username' and 'password' field, named as is. I can add a user and my password is successfully getting hashed and stored in my table. Despite this, I am unable to log in even once.

My AppController initialize() method:

$this->loadComponent('Auth',['loginAction' => [
                            'controller' => 'ShopOwners',
                            'action' => 'login'
                         ],'authenticate' => [
                              'Form' => [
                                'fields' => [
                                  'username' => 'username',
                                  'password' => 'password',
                                 ],
                                 'userModel' => 'ShopOwners'
                               ]
                         ],'loginRedirect' => [
                             'controller' => 'ShopOwners',
                             'action' => 'view'
                         ],
                    ]);
$this->Auth->allow(['home','add']);

My login view function in my Controller

public function login(){
    $this->log('Inside login','debug');
    if($this->request->is('post')){
       $this->log('Inside login is post','debug');
       $shopOwner = $this->Auth->identify();
       if($shopOwner){
          $this->log('Inside is owner','debug');
          $this->Auth->setUser($shopOwner);
          return $this->redirect($this->Auth->redirectUrl());
       }
      $this->Flash->error(__('Invalid username or password, try again'));
    }
}

And finally my login view file login.ctp

<h1>Login</h1>
<?= $this->Form->create(); ?>
<?= $this->Form->input('username'); ?>
<?= $this->Form->input('password'); ?>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end(); ?>

I have tried many solutions offered on the site and elsewhere to try and fix this. These include:

  1. protected $_accessible is set corrrectly
  2. Checked that table column names are username and password exactly
  3. SQL query is returning 1 row, as checked through the Console, but Auth->identify() is not returning anything.
  4. Made many new users in case I had entered the wrong password, no luck nothing cleared
  5. Checked my file/table/column names to make everything matches[at least I think it's all fine]
  6. Also checked if the user is already logged in by using $this->Auth->user('username') which returned nothing

New to Cakephp, and have been stuck at this one for a whole day now. Really appreciate any help.

EDIT: Can someone at least tell me why identify() is not returning anything. I did a trace in my login function in the controller and it returned

App\Controller\ShopOwnersController::login() - 
 APP/Controller\ShopOwnersController.php, line 132
Cake\Controller\Controller::invokeAction() - 
 CORE\src\Controller\Controller.php, line 405
Cake\Routing\Dispatcher::_invoke() - 
 CORE\src\Routing\Dispatcher.php, line 114
Cake\Routing\Dispatcher::dispatch() - 
  CORE\src\Routing\Dispatcher.php, line 87
[main] - ROOT\webroot\index.php, line 37

Ends with a:

Undefined variable: _SESSION [CORE\src\Network\Session.php, line 436]

Why is it stopping at index.php? What could be wrong? In the index I checked

print_r(Request::createFromGlobals());

In which I can see my data array contain the username and password, but still nothing. The password is text and not hashed, is that the problem??

EDIT 2: My Request Object contains the following

[params] => Array ( [plugin] => [controller] => [action] => [_ext] => [pass] => Array ( ) ) 
[data] => Array ( [username] => user9 [password] => user9 ) 
[query] => Array ( )

Why is my query Array empty? Is it supposed to be??

EDIT 3 - I tried to send in a hashed version of my password and reset request->data['password'] to the hashed version. This did not work because

  1. Hashed versions for the same word were different each time. I used (new DefaultPasswordHasher)->hash() for this
  2. I tried to copy paste the hashed version of the password from the database directly into my login form. No luck
1
Added my trace aboveKVNam
What is the size of the password column in your database?José Lorenzo Rodríguez
This is extremely embarrassing, I saw the solution to increase the password col size in Github, offered by you, and then proceeded to increase username size by accident and drive myself mad. Thank you so much. I have now increased password size to 255, and it works. :/ Is it alright if I mark your comment as the right answer?KVNam
I will edit my answer to reflect my commentJosé Lorenzo Rodríguez
Thanks again. Answer selected.KVNam

1 Answers

4
votes

If you are not using a table called "Users" for storing the username and the password, you need to tell CakePHP about it, as explained in this section of the manual:

http://book.cakephp.org/3.0/en/controllers/components/authentication.html#configuring-authentication-handlers

Basically you need to add this to your AuthComponent config:

$this->loadComponent('Auth',['loginAction' => [
                        'controller' => 'ShopOwners',
                        'action' => 'login'
                     ],'authenticate' => [
                          'Form' => [
                            'userModel' => 'ShopOwners', // Added This
                            'fields' => [
                              'username' => 'username',
                              'password' => 'password',
                             ]
                           ]
                     ],'loginRedirect' => [
                         'controller' => 'ShopOwners',
                         'action' => 'view'
                     ],
                ]);

One important thing to remember is that password hashes are usually very long, so make sure your password column in the database is big enough (I use 255 just in case)