I have a CakePHP 2.0 app that uses the AuthComponent with User model to successfully login to manage posts and other controllers and models. I'll call that the "super admin" part of the site.
However, I need to create an entirely separate part of the site for advertisers. I have created model Advertiser and controller AdvertisersController. I have also created table advertisers. I am able to successfully add an advertiser user, but when I attempt to login, it fails with the invalid username or password error.
I followed suggestions in the Cookbook for Authentication and tried both of these options in the beforeFilter of my AdvertisersController:
$this->Auth->authenticate = array(
AuthComponent::ALL => array(
'userModel' => 'Advertiser',
'fields' => array('username' => 'username', 'password' => 'password'),
'Form',
'Basic'
));
$this->Auth->authenticate = array(
'Form' => array('userModel' => 'Advertiser'),
'Basic' => array('userModel' => 'Advertiser')
);
Any ideas? Is it not possible to use the AuthComponent with two different models/controllers?
EDIT: I updated my AdvertisersController to this:
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'Advertiser'),
'Form',
'Basic'
);
$this->Auth->allow('add', 'logout'); // Letting advertisers register themselves
}
Now the login is failing altogether -- no failed password error. I even tried a bogus username and password. Here is the login method in my controller:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect(array('controller' => 'advertisers', 'action' => 'admin'));
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
I added an else print_r($this->data); exit; statement after the close of the first if and got an empty array. Couldn't even get to the login page.
Also, the output for the form is this -- POST is the method.
<form action="/advertisers/login?url=advertisers%2Flogin" id="AdvertiserLoginForm" method="post" accept-charset="utf-8">
<div style="display:none;"><input type="hidden" name="_method" value="POST"/></div>
<fieldset>
<legend>Please enter your username and password</legend>
<div class="input text required">
<label for="AdvertiserUsername">Username</label>
<input name="data[Advertiser][username]" maxlength="50" type="text" id="AdvertiserUsername"/>
</div>
<div class="input password required">
<label for="AdvertiserPassword">Password</label>
<input name="data[Advertiser][password]" type="password" id="AdvertiserPassword"/>
</div>
</fieldset>
<div class="submit">
<input type="submit" value="Login"/>
</div>
</form>