Cake PHP 2 : Login

  1. For login here we are using Auth component
    public function login() {
    if ($this->request->is(‘post’)) {
    if ($this->Auth->login()) {
    return $this->redirect(array(‘controller’ => ‘test’, ‘action’ => ‘index’));
    } else {
    $this->Flash->error(__(‘Invalid Email or Password, please try again’));
    return $this->redirect(array(‘action’ => ‘login’));
    }
    }
    if ($this->Auth->login()) {
    return $this->redirect($this->Auth->redirectUrl());
    }
    }
  • Set component into app controller
    e.g. public $components = array(‘Flash’,’Session’,’Auth’);

  • encrypt password in before save method which we declare into Model