How to remove the 'Invalid username or password, try again' message from the CakePHP login page?

2

I started using the recently and I created a page from login to my project. The login works perfectly, but is always displaying an invalid user or password.

What can I do to display this message ONLY when login is invalid?

Login function:

public function login() {
        if ($this->Auth->login()) {
            if($this->Auth->user('role') === 'paciente') {
                $this->redirect(array('controller' => 'pacientes', 'action' => 'index'));
            }
            elseif($this->Auth->user('role') === 'medico') {
                $this->redirect(array('controller' => 'medicos', 'action' => 'index'));
            }
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
    
asked by anonymous 11.09.2015 / 19:41

1 answer

2

You can check if the page request is a post with $this->request->is('post');

public function login() {
    if($this->request->is('post')){
        if ($this->Auth->login()) {
            if($this->Auth->user('role') === 'paciente') {
                $this->redirect(array('controller' => 'pacientes', 'action' => 'index'));
            }
            elseif($this->Auth->user('role') === 'medico') {
                $this->redirect(array('controller' => 'medicos', 'action' => 'index'));
            }
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }

}

Documentation Reference this link

    
11.09.2015 / 19:49