Create simple user authentication using CakePHP’s Auth component

Been quite some days since the last time I sit in front of my computer and typing all the “echo”, “print_r” etc. As required in works, I’m punching my keyboards again today, researching on how to do simple authentication using CakePHP’s Auth component.
Suppose to be a fairly simple task, but weird that I can’t find a clear/good tutorial on this, despite the size of its community. However, after few hours of trial and error and reading of all sort of tutorials from several authors, finally managed to get it done. Cheers~
SQL to create table: users
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(255) NOT NULL,
`password` char(40) NOT NULL,
`group_id` int(11) NOT NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
To create a registration form
Please take note that, cakephp will automatically hash the password

1. Crease model: user.php
<?php
class User extends AppModel {
var $name = ‘User’;
var $useTable = ‘users’;
}
?>
2. Create controller: users_controller.php
class UsersController extends AppController {
var $name = ‘Users’;
var $helpers = array(‘Html’, ‘Form’);
var $components = array(‘Auth’);
function beforeFilter() {
$this->Auth->allow(‘register’);
}
function register() {
if (!empty($this->data)) {
if ($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])) {
$this->User->create();
$this->User->save($this->data);
$this->redirect(array(‘action’ => ‘index’));
}
}
}
}
3. Create view: users\register.ctp
<?php
echo $form->create(‘User’, array(‘action’ => ‘register’));
echo $form->input(‘username’);
echo $form->input(‘password’);
echo $form->input(‘password_confirm’, array(‘type’ => ‘password’));
echo $form->submit();
echo $form->end();
?>
Sourced from: http://www.littlehart.net/atthekeyboard/2008/01/08/simple-user-registrat...
To enable authentication for everything, and request to log in shall users try to enter straight
1. Create view: users\login.ctp
Quote
<?php
echo $form->create(‘User’, array(‘action’=>’login’));
echo $form->input(‘username’);
echo $form->input(‘password’);
echo $form->end(‘Log In Now’);
?>
2. Create app_controller.php under folder app/
Quote
<?php
class AppController extends Controller{
var $components = array(‘Auth’);
function beforeFilter(){
$this->Auth->authorize = ‘controller’;
$this->Auth->loginAction = array(‘controller’=>’users’, ‘action’=>’login’);
$this->Auth->loginRedirect = array(‘controller’=>’users’, ‘action’=>’index’);
$this->Auth->logoutRedirect = ‘/’;
}
function isAuthorized(){
return true;
}
}
?>
Sourced from: http://www.littlehart.net/atthekeyboard/2007/09/11/a-hopefully-useful-tu...
Sourced from: http://book.cakephp.org/complete/218/Tutorials-Examples
To create log out function
1. Add function: logout into users_controller.php
Quote
function logout() {
$this->Session->setFlash(“You’ve successfully logged out.”);
$this->redirect($this->Auth->logout());
}