i have problem in login form $this->Auth->login() always returns false. i am using different table name for user account. the below is my code
HgAdminscontroller.php
<?php
// app/Controller/UsersController.php
class HgAdminsController extends AppController
{
public function beforeFilter()
{
parent::beforeFilter();
//$this->Auth->allow('add', 'logout');
}
public function index() {
$this->HgAdmin->recursive = 0;
$this->set('users', $this->paginate());
}
/*
public function add() {
if ($this->request->is('post')) {
$this->HgAdmin->create();
if ($this->HgAdmin->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}*/
public function login() {
if ($this->request->is('post')) {
echo AuthComponent::password($this->data['password']);
debug($this->data);
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
?>
appController.php
App::uses('Controller', 'Controller');
/**
* Application Controller
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @package app.Controller
* @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
*/
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
// Security::setHash('md5');
$this->Auth->userModel = 'hg_admins';
$this->Auth->loginAction = array( 'controller' => 'HgAdmins', 'action' => 'login' );
}
//...
}
HgAdmin.php //model
<?php
class HgAdmin extends AppModel {
var $name= "hg_admin";
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
)
);
?>
Login view
<h2>Login Here</h2>
<?php echo $this->Session->flash('auth');
echo $this->form->create('HgAdmin');
echo $this->Form->input("username");
echo $this->Form->input("password");
echo $this->form->end(__("Login"));
?>
i have checked the database password value is same as comming from AuthComponent::password($this->data[‘password’]); so dont know why it is fialing. if i use add function of cakephp to add user the user is added with the same password but not able to login with that.
You have to use the name of the Model on which you want to apply authentication i.e i ahve used Writer for my table writers in db.Check changing that!