We can use Auth component in cakephp for authentication and authorization objects. Since it easy and secure way to do this.
Write following in your App controller
public $components = array(
'Auth' => array('loginAction' => array('controller' => 'users', 'action' => 'login'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'authError' => 'You don\'t have access here.',
)
);
Following in your User controller
//LOGIN
public function login() {
if ($this->request->is('post')) {
$this->Auth->authenticate = array(
'Form' => array(
'userModel' => 'TableName',//Employer,Athlete or etc
)
);
if ($this->Auth->login()) {
$this->redirect(array('controller' => 'users', 'action' => 'profile'));
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
$this->redirect(array('controller' => 'Pages', 'action' => 'home#toLogin'));
}
}
}
//LOGOUT
public function logout() {
$this->Auth->autoRedirect = false;
$this->Auth->logout();
$this->redirect(array('controller' => 'pages', 'action' => 'home'));
}
0 Comment(s)