I am working on a simple log in application. Whenever I log in it prints the error message: Invalid Username or password. I tried debug($this->Auth->login()) but it always returns true and despite this log in gets failed. Another problem is if I put $this->request->data inside login(),it logs in even with wrong password. Following is the code. AppController.php
App::uses('Controller', 'Controller');
class AppController extends Controller
{
public $components = array(
'Session',
'Auth' => array('loginAction'=>array('controller'=>'students','action'=>'login'),
'loginRedirect' => array('controller' => 'students','action' => 'add'),
'logoutRedirect' => array('controller' => 'students','action' => 'login'),
'authError'=> 'You must be logged in to view this page!',
'loginError'=> 'Galat Username or Password !',
'authenticate' => array('Form' => array('userModel' => 'Student','fields' => array('username' => 'email','password'=>'password')),
'authorize'=>'Controller'
)
);
public function beforeFilter() {
$this->Auth->userModel='Student';
$this->Auth->allow('login','logout');
}
}
StudentsController.php is:
<?php
App::uses('AppController', 'Controller');
class StudentsController extends AppController {
public $helpers = array('Session', 'Number', 'Text', 'Time');
public $components = array('Paginator', 'Auth', 'Flash', 'Security', 'Session');
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('login','add','index');
}
public function login()
{
//if already logged-in, redirect
/*if($this->Session->check('Auth.Student'))
{
$this->redirect(array('action'=>'index'));
}*/
//if we get the post information,try to authenticate
if($this->request->is('post'))
{
if(debug($this->Auth->login()))
{
$this->Session->setFlash(__('Welcome, '.$this->Auth->user('name')));
/*$this->redirect($this->Auth->redirectUrl());*/
}
else
{
$this->Session->setFlash(__('Invalid username or password from student controller!'));
}
}
}
Login view is:
<div class="users form" >
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Student'); ?>
<fieldset >
<legend>
<?php echo __('Please enter your email and password.'); ?>
</legend>
<?php echo $this->Form->input('email');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
<?php
echo $this->Html->link("Register",array('action'=>'add'));
echo"\r\n";
echo $this->Html->link("Forgot Password",array('action'=>'add'));
?>
Student.php
<?php
App::uses('AppModel', 'Model');
/**
* Student Model
*
*/
class Student extends AppModel {
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'enroll_no';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'enroll_no' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter your enrollment number',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter your full name.',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'course' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter your course e.g. B.Tech MCA',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'majors' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'year_of_admission' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'email' => array(
'email' => array(
'rule' => array('email'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'phone_no' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'guardian_name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, //Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'guardian_phone_no' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, //Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
)
);
public function beforeSave($options = array()) {
/* password hashing */
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}