1
votes

I am implementing authentication in a cakephp project. But I don't want to use the default "users" table. I want to use "system_admins" table. I read a tutorial here: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers.

Here is my controller (SysemAdminController.php)

<?php
App::uses('AppController', 'Controller');

class SystemAdminsController extends AppController {
    // index, add, edit, delete actions
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->authenticate = array(
            AuthComponent::ALL => array('userModel' => 'SystemAdmin'),
            'Form',
            'Basic'
       );
    }

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

And here is model (SystemAdmin.php)

<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
// validates options
public function beforeSave($options = array()) {
    if(isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
}

Here is view (login.ctp)

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('SystemAdmin'); ?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
<?php echo $this->Form->input('username');
      echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>

But whenever I try to go to http://mydomain.com/system_admins/login it always redirects me to http://mydomain.com/users/login and show me the error message: "UsersController could not be found."

1
Set $this->Auth->loginRedirect and $this->Auth->logoutRedirect (book.cakephp.org/2.0/en/core-libraries/components/…)kicaj

1 Answers

1
votes

You have not defined the default login page. By default, the user model is User, so the default login page is /users/login.

You need to define the loginAction: $this->Auth->loginAction = '/system_admins/login';

And probably allow the login page for non authenticated users: $this->Auth->allow('login');