Ok, first clarify how AuthComponent :: login works. In Cake 2.x, this method does not perform any authentication, but creates an Auth.User array in your session. You need to implement authentication itself (the User model is the natural place to do this). The basic authentication method might look like this:
App::uses('AuthComponent', 'Controller/Component'); public function authenticate($data) { $user = $this->find('first', array( 'conditions' => array('User.login' => $data['login']), )); if($user['User']['password'] !== AuthComponent::password($data['password']) { return false; } unset($user['User']['password']); // don't forget this part return $user; // the reason I return the user is so I can pass it to Authcomponent::login if desired }
Now you can use this from any controller while the user model is loaded. You may know that you can load it by calling Controller::loadModel('User') .
If you want to authenticate each request, then you must enter the beforeFilter method for the AppController:
public function beforeFilter() { $this->loadModel('User'); if(!$this->User->authenticate($this->request->data)) { throw new UnauthorizedException(__('You don\'t belong here.')); } }
All of the above assumes that you pass POST values ββfor login and password each time. I think token authentication is definitely the best way to go, but in order to get up and running, this should work. Some of the disadvantages include sending a password to cleartext (if you do not need ssl) every request and probably a high level of use of the hashing algorithm every time. However, I hope this gives you a better idea of ββhow to do authentication with cakephp.
Let me know if something needs clarification.
Update: After posting this question, I found out that you can use AuthComponent :: login without parameters, but I'm not a fan of this. From the CakePHP documentation:
In 2.x $this->Auth->login($this->request->data) will log the user in with whatever data is posted, whereas in 1.3 $this->Auth->login($this->data) would try to identify the user first and only log in when successful.
source share