Cara membuat simple auth sederhana
Cara membuat simple auth sederhana
1.
Tambahkan public komponen
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display','home')));
public function beforeFilter(){
$this->Auth->allow('*');
}
2.
Di user controller tambahkan kode public function login() and Logout ()
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow(''); // Letting users register themselves
}
public function login(){
$this->layout='loginadmin';
if ($this->request->is('post')){
if ($this->Auth->login()){
$this->redirect($this->Auth->redirect('users/index'));
} else {
$this->Session->setFlash(__('Invalidusernameorpassword,tryagain'));
}
}
}
public function logout(){
$this->redirect($this->Auth->logout());
}
3. Kemudian pada table user model script dibawah ini
public $validate = array(
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'password' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'role' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//app/Model/User.php
//App::uses(’AuthComponent’, ’Controller/Component’);
//class User extends AppModel{
////...
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;
}
4. Untuk database users dengan format seperti di bawah ini
5. Untuk controller lain2nya silahkan tambahkan before filter function login N logout seperti langkah yang ke 3.
Semoga berhasil.. ini catatan