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:
- protected $_accessible is set corrrectly
- Checked that table column names are username and password exactly
- SQL query is returning 1 row, as checked through the Console, but Auth->identify() is not returning anything.
- Made many new users in case I had entered the wrong password, no luck nothing cleared
- Checked my file/table/column names to make everything matches[at least I think it's all fine]
- 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
- Hashed versions for the same word were different each time. I used (new DefaultPasswordHasher)->hash() for this
- I tried to copy paste the hashed version of the password from the database directly into my login form. No luck