How to remove 'Invalid username or password, Try Again' message from Cakephp login page?

Asked

Viewed 268 times

2

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

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

Function of login:

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'));
        }
    }

1 answer

2

You can check if the page request is a post with the $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 at this link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.