php - CakePHP login issue -
stackoverflowers. meet issue while add login function cakephp website, account can added, user can not login system
appcontroller:
namespace app\controller; use cake\controller\controller; class appcontroller extends controller { public function initialize() { $this->loadcomponent('flash'); $this->loadcomponent('auth', [ 'authenticate' => [ 'form' => [ 'fields' => [ 'username' => 'email', 'password' => 'password' ] ] ], 'loginaction' => [ 'controller' => 'users', 'action' => 'login' ] ]); // allow display action our pages controller // continues work. $this->auth->allow(['display']); }
userscontroller:
<?php namespace app\controller; use app\controller\appcontroller; /** * users controller * * @property \app\model\table\userstable $users */ class userscontroller extends appcontroller { /** * index method * * @return void */ public function index() { $this->set('users', $this->paginate($this->users)); $this->set('_serialize', ['users']); } /** * view method * * @param string|null $id user id. * @return void * @throws \cake\network\exception\notfoundexception when record not found. */ public function view($id = null) { $user = $this->users->get($id, [ 'contain' => ['backlogorders', 'orders'] ]); $this->set('user', $user); $this->set('_serialize', ['user']); } /** * add method * * @return void redirects on successful add, renders view otherwise. */ public function add() { $user = $this->users->newentity(); if ($this->request->is('post')) { $user = $this->users->patchentity($user, $this->request->data); if ($this->users->save($user)) { $this->flash->success(__('the user has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->flash->error(__('the user not saved. please, try again.')); } } $this->set(compact('user')); $this->set('_serialize', ['user']); } /** * edit method * * @param string|null $id user id. * @return void redirects on successful edit, renders view otherwise. * @throws \cake\network\exception\notfoundexception when record not found. */ public function edit($id = null) { $user = $this->users->get($id, [ 'contain' => [] ]); if ($this->request->is(['patch', 'post', 'put'])) { $user = $this->users->patchentity($user, $this->request->data); if ($this->users->save($user)) { $this->flash->success(__('the user has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->flash->error(__('the user not saved. please, try again.')); } } $this->set(compact('user')); $this->set('_serialize', ['user']); } /** * delete method * * @param string|null $id user id. * @return void redirects index. * @throws \cake\network\exception\notfoundexception when record not found. */ public function delete($id = null) { $this->request->allowmethod(['post', 'delete']); $user = $this->users->get($id); if ($this->users->delete($user)) { $this->flash->success(__('the user has been deleted.')); } else { $this->flash->error(__('the user not deleted. please, try again.')); } return $this->redirect(['action' => 'index']); } 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 incorrect.'); } } public function beforefilter(\cake\event\event $event) { $this->auth->allow(['add']); } }
phpmyadmin table: 3 column: id email password
password hashing used.
thank you!
the 'auth' component configured in appcontroller. properties need configured 'authenticate', 'loginaction', 'loginredirect' , 'logoutredirect'.
'loginredirect' => [ 'controller' => 'pages', 'action' => 'display', 'pass[0]' => 'user_home', ], 'logoutredirect' => [ 'controller' => 'users', 'action' => 'login', ],
'loginredirect' page users landed when login. 'logoutredirect' page users landed after logout.
Comments
Post a Comment