Alimon Karim
Alimon Karim

Reputation: 4469

CakePHP : 2.4 Auth component not working

I have a table name admin_users, I am trying to use auth component here. So I have written in appcontroller bellow code

public $components = array('Session','RequestHandler','Paginator'=>array('limit'=>4),'Auth'=>array(
        'loginAction'=>array(
        'controller'=>'adminusers',
        'action'=>'login'
        ),
        'loginRedirect' => array('controller' => 'adminusers','action' => 'index'),
        'logoutRedirect' => array('controller' => 'adminusers','action' => 'index'),
        'authError'=>'You can not access this page!!',
        )); 

For set user I have written

public function beforeFilter() {
    //$this->Auth->allow();
    $this->set('logged_in', $this->Auth->loggedIn());
    $this->set('current_user',$this->Auth->user());
     parent::beforeFilter();
        $this->Paginator->settings = array(
          'limit'=>4
    );


}

I have made a method call login() in adminusers controller

    public function login() {
    $this->layout = 'login';
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}

For hash password I have written in AdminUser model

    public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $passwordHasher = new SimplePasswordHasher();
        $this->data[$this->alias]['password'] = $passwordHasher->hash(
            $this->data[$this->alias]['password']
        );
    }
    return true;
}

This is the login.ctp

              <?php echo $this->Form->create('AdminUser'); ?>
              <?php 
                    echo $this->Form->input('username',array(
                        'label' => false,
                        'placeholder'=>'UserName'
                        ));
                    echo $this->Form->input('password',array(
                        'label' => false,
                        'placeholder'=>'Password'
                    ));
                ?>

After used allow() mathod, I have created a user and here hash password working fine.But the problem is when I am trying to login, It is giving me "Invalid username or password, try again".

Upvotes: 0

Views: 184

Answers (1)

conehead
conehead

Reputation: 135

In your login.ctp you create an AdminUser. Auth uses the model 'User' as default. You need to define your own model in the Auth-Component.

 public $components = array('Session','RequestHandler','Paginator'=>array('limit'=>4),'Auth'=>array(
    'loginAction'=>array(
    'controller'=>'adminusers',
    'action'=>'login'
    ),
    'loginRedirect' => array('controller' => 'adminusers','action' => 'index'),
    'logoutRedirect' => array('controller' => 'adminusers','action' => 'index'),
    'authError'=>'You can not access this page!!',
    'authenticate' => array(
        'Form' => array(
            'userModel' => 'AdminUser',
            'passwordHasher' => 'Blowfish'
        )
    )
)); 

Upvotes: 1

Related Questions