One of the parts of CakePHP that caused me the most grief while trying to learn the finer details of the framework was the Auth component. The problem at the time was that there was scant information available on how to actually use and configure it. Luckily, I was able to get some insider information via gwoo and Nate from the CakePHP core team. Plus there was stuff floating around on different web sites. So, here I will attempt to collect some of that information and show you some basic-to-intermediate info on how to use the component. Note, this code is for 1.2.x.x, and the API for the Auth component had not been declared 100% stable at the time of this post. In other words, don’t blame me if this doesn’t work 6 months from now. :)
So, in order to use this component, you have to add it to the list of components your controller is using. I tend to put this info in app_controller.php since I usually need Auth to work for all my controllers.
1
var $components = array('Auth');
1
$this->Auth->fields = array('username' => 'email', 'password' => 'pasword');
1
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
1
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
1
$this->Auth->logoutRedirect = '/';
1
$this->Auth->loginError = 'Invalid e-mail / password combination. Please try again';
1
$this->Auth->autoRedirect = false;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function login() {
if ($this->Auth->user()) {
if (!empty($this->data)) {
if (empty($this->data['User']['remember_me'])) {
$this->Cookie->del('User');
}
else {
$cookie = array();
$cookie['email'] = $this->data['User']['email'];
$cookie['token'] = $this->data['User']['pasword'];
$this->Cookie->write('User', $cookie, true, '+2 weeks');
}
unset($this->data['User']['remember_me']);
}
$this->redirect($this->Auth->redirect());
}
}
$this->Auth->authorize = 'controller';
1
2
3
4
5
6
7
8
9
function isAuthorized() {
if (isset($this->params[Configure::read('Routing.admin')])) {
if ($this->Auth->user('admin') == 0) {
return false;
}
}
return true;
}
1
$this->Auth->mapActions(array('read'=> array('display')));
1
$this->Auth->allow('add', 'view');
Now, there are also some other features that are useful to those using Auth:
1
2
3
4
function logout() {
$this->Session->setFlash("You've successfully logged out.");
$this->redirect($this->Auth->logout());
}
1
$userId = $this->Auth->user('id');
1
2
3
if ($this->Auth->user('admin') == true) {
$this->Session->setFlash('You are an admin');
}
Phew. That’s a lot of stuff to try and keep in your head when building apps using Auth. But there’s still more. The Auth component will automatically hash whatever value you place in the ‘password’ field in your form. It will do this both upon login *and* if you are creating a record in your user model, so you need to make sure that your field in your table that stores the password is large enough that it will accept the hashed password. If you need to do something with that password before you store it, call the field something other than what you’ve told Auth your password field is, then you can get the value needed to store it by using $this->Auth->password(‘passwordtostorehere’).
Anyhow, I’m sure I’ve missed some other more esoteric stuff, but I hope I’ve done a good job of showing just how easy the Auth component really is to use…once you understand how it actually works. :)