AppController
class AppController extends Controller { public $components = array( 'Session', 'Auth' => array( 'loginRedirect' => array('controller' => 'users', 'action' => 'index'), 'logoutRedirect' => array('controller' => 'users', 'action' => 'index'), ) ); }
UsersController
class UsersController extends AppController { public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow('add'); } public function login() { if ($this->request->is('post')) { if ($this->Auth->login()) { $this->redirect($this->Auth->redirect()); } else { $this->Session->setFlash(__('Invalid username or password, try again')); } } } public function logout() { $this->redirect($this->Auth->logout()); } public function top_page(){ }
User(Model)
App::uses('AppModel', 'Model','AuthComponent', 'Controller/Component'); public function beforeSave($options = array()) { if (isset($this->data[$this->alias]['password'])) { $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); } return true; }
login.ctp
<div class="users form"> < ?php echo $this->Session->flash('auth'); ?> < ?php echo $this->Form->create('User'); ?> < ?php echo $this->Form->input('username'); echo $this->Form->input('password'); ?> < ?php echo $this->Form->end(__('Login')); ?> </div>