A while back (when I had very little experience with cakephp), I wrote about how to go about setting user authentication in your cakephp. Basically you have to login to do anything at all in my whole web app. So we manually set the user id and stuff in the sessions and check it every now and then. Now I'm more experienced and have finally been able to google my way to a better understanding of how it can be done using the auth component that comes with cakephp.
First is that it is a component. And you will want it everywhere in your application. So where else do you write it but in your app/app_controller.php file.
var $components = array('Acl','Auth');
Make sure you add Acl before Auth because Auth uses some functions in Acl which is not initialized unless it is already loaded. So load Acl first.
Okay, then you have to set up some settings for your Auth component. It is set in the beforeFilter function of your app_controller.
function beforeFilter() {
$this->Auth->loginAction=array('controller'=>'users','action'=>'login');
$this->Auth->loginRedirect=array('controller'=>'users','action'=>'alert');
$this->Auth->logoutRedirect=array('controller'=>'users','action'=>'login');
$this->Auth->loginError=__('Invalid username or password',true);
$this->Auth->authorize='controller';
}
Okay. Some explanation. loginAction is which page to display when your user will login. Of course it has to be a page with a form with the fields username and password. Then loginRedirect is the default page to go to if the users come directly to the login page. Let me explain. You see, when parts of your application is protected with this component, and the user has not logged in, the user will be redirected automatically to the login page to allow the user to login. If it is a successful login s/he will be directed back to the page s/he requested for before. Unless the user went directly to the login page. Because then the component don't know where to send the user. So we set it using loginRedirect. logoutRedirect is the page to send the user to once they have successfully logged out. In my case I'd send them straight back to the login page. loginError is of course the error message that will be flashed if the user didn't login successfully. And finally authorize will determine who will authorize the user. It can be set to 'controller', 'actions', 'crud', array('model'=>'name') or 'object'. To learn more see here.
Then if you want your views to be able to know who is logged in you've got to send the data over. I do it in the beforeRender function like this.
function beforeRender(){
if($this->Auth->user()){
$this->set('auth_user',$this->Auth->user());
}
}
Pretty straight forward. Previously I've set the controller to authorize the user access, so we will need the isAuthorized function defined in our controller. Define it in our app_controller like this.
function isAuthorized(){
return true;
}
Then overwrite in your model controllers for more sophisticated authentication. Basically I've just set default is to allow access to the controller.
Okay. That's it for the app_controller. Now for the user controller.
function login(){
}
function logout(){
$this->Session->SetFlash(__('Successfully logged out',true));
$this->redirect($this->Auth->logout());
}
Make sure the login view is created. But no other code is necessary for the login. And very litte required for the logout function. And basically that is it. Very cool. Of and one more thing. The auth component will actually hash the password with some other measures of security. So you have to make sure at least the first user uses the same password generated by $this->Auth->password('password') so that you can login. Or some other way to get the password in. I've read that when you save the user data the auth component will do it for you, haven't tested that yet. But sure to try soon.
