0
votes

Hello everyone I have tried the basic auth login and its working fine but when used with admin prefix the

 $this->Auth->login() is returning false. 

here are my code snippets,

In Model i have added,

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

In AppController I added the following code,

public $components = array(
            'Session',     
            'Auth' => array(
                'loginRedirect' => array('controller' => 'users', 'action' => 'index', 'admin' => true),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'login', 'admin' => true)
            )

        );

In UsersController my admin login is as follows,

public function admin_login() {
        if ($this->request->is('post')) {
            //$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
            debug($this->request->data['User']['password']);
            debug($this->Auth->login());
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }

In the above,

debug($this->request->data['User']['password']) is returning the correct password but
debug($this->Auth->login()) is returning false.

I am not able to figure out whats the reason it is returning false, is there any changes that needs to be done for admin routing.

1
I know this has been a long time now, but may I know how you fixed this? I'm having the same problem. It was working before but it's been awhile since I visited the admin page and now it's not working. Appreciate your reply. - Leah

1 Answers

0
votes

In routes.php You can write this code :

Router::connect('/admin', array('admin' => true, 'controller' => 'users', 'action' => 'login'));

and In AppController.php

public $components = array(
      'Session',     
      'Auth' => array(
            'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
      )
);

Try this.