mysql - Cake php login says invalid username and password even when they are correct -
i trying implement login system cake php blog tutorial own system cannot seem working. attempts made login met error set in usercontroller->login().
heres of code, can post more if needed.
userscontroller.php
namespace app\controller; use app\controller\appcontroller; use cake\event\event; class userscontroller extends appcontroller { public function beforefilter(event $event) { parent::beforefilter($event); $this->auth->allow(['add','logout']); } 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(__('invalid username or password, try again.')); } } appcontroller.php
class appcontroller extends controller { public function initialize() { parent::initialize(); $this->loadcomponent('requesthandler'); $this->loadcomponent('flash'); $this->loadcomponent('auth', [ 'authenticate' => [ 'form' => [ 'fields' => [ 'username' => 'username', 'password' => 'password' ] ] ], 'loginredirect' => [ 'controller' => 'projects', 'action' => 'index' ], 'logoutredirect' => [ 'controller' => 'pages', 'action' => 'display', 'home' ] ]); } public function beforefilter(event $event) { $this->auth->allow(['index', 'view', 'edit', 'display']); } } login.ctp
<div class="users form"> <?= $this->flash->render('auth') ?> <?= $this->form->create() ?> <fieldset> <legend><?= __('please enter username , password') ?></legend> <?= $this->form->input('username') ?> <?= $this->form->input('password') ?> </fieldset> <?= $this->form->button(__('login')); ?> <?= $this->form->end() ?> </div> user.php
class user extends entity { protected $_accessible = [ '*' => true, 'id' => false, ]; protected function _setpassword($password) { return (new defaultpasswordhasher)->hash($password); } } userstable.php
class userstable extends table { public function initialize(array $config) { parent::initialize($config); $this->table('users'); $this->displayfield('username'); $this->primarykey('id'); $this->addbehavior('timestamp'); $this->hasmany('projects', [ 'foreignkey' => 'user_id' ]); $this->hasmany('ticketscomments', [ 'foreignkey' => 'user_id' ]); $this->belongstomany('projects', [ 'foreignkey' => 'user_id', 'targetforeignkey' => 'project_id', 'jointable' => 'projects_users' ]); $this->belongstomany('tickets', [ 'foreignkey' => 'user_id', 'targetforeignkey' => 'ticket_id', 'jointable' => 'tickets_users' ]); } public function validationdefault(validator $validator) { $validator ->integer('id') ->allowempty('id', 'create'); $validator ->requirepresence('username', 'create') ->notempty('username', 'a username reuired.') ->add('username', 'unique', ['rule' => 'validateunique', 'provider' => 'table']); $validator ->email('email') ->requirepresence('email', 'create') ->notempty('email') ->add('email', 'unique', ['rule' => 'validateunique', 'provider' => 'table']); $validator ->requirepresence('password', 'create') ->notempty('password', 'a password required.'); $validator ->notempty('role', 'a role required.') ->add('role', 'inlist', [ 'rule' => ['inlist',['admin', 'user']], 'message' => 'please enter valid role.' ]); return $validator; } public function buildrules(ruleschecker $rules) { $rules->add($rules->isunique(['username'])); $rules->add($rules->isunique(['email'])); return $rules; } } i pretty baffled have done wrong. can confirm in database passwords hashing. read somewhere passwords should varchar(50) in database , case mine shouldn't that.
thanks in advance
this not work right away, form being posted? need start debugging, change function this, check:
public function login() { if($this->request->is('post')){ echo "yes, have posted information!<br />"; var_dump($this->request); $user = $this->auth->identify(); echo "<br /><br />let's have @ $user = $this->auth->identify();"; var_dump($this->request); if($user){ $this->auth->setuser($user); return $this->redirect($this->auth->redirecturl()); } } $this->flash->error(__('invalid username or password, try again.')); }
Comments
Post a Comment