0
votes

I have made a a basic login form with three fields, they're "company", "employee", and "password" I tried using the default Auth component:

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'));
        }
    }
}

but as this parses the default "username", and "password" fields I can't log in. How can I change the Auth function to validate my three fields and log me in?

Sorry if this is a noob question, I had a read through http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html but nothing on there helped.

1

1 Answers

1
votes

Try the following:

public function beforeFilter() {
    $this->Auth->authenticate = array(
        'Form' => array(
            'fields' => array(
                'username' => 'employee', 
            ),
        ),
    );
}

public function login() {
    if ($this->request->is('post')) {
        $this->Auth->authenticate['Form']['scope'] = array(
            'User.company_id' => $this->request->data['User']['company_id'],
            // or however this condition needs to be setup as
        );
        if ($this->Auth->login()) {
             // login success
        } else {
             // login fail
        }
    }
}

Basically, add the third condition as a scope.