Invalid login message always appears in CakePHP

1

When I enter my login page, the warning message that the user or passwords are invalid appears soon, but this should only appear after clicking the button to sign in. Can not you get around this?

My view:

    <div class="large-4 columns" style="margin-left: 15px;">
     <div class="row">
     <div class="users form">
       <span class="login_form">
        <?php echo $this->Session->flash(); ?></span>
   <?php echo $this->Form->create('User');?>
    <fieldset>
        <?php echo $this->Form->input('email');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->submit('Login', array('class' => 'button'));?>
</div>
</div>

My action:           public function login () {

    if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash(__('Username ou password inválidos!'));
    }
}
    
asked by anonymous 28.02.2014 / 11:12

1 answer

4

It's because your action is trying to authenticate as soon as it loads.

// Verifica o tipo de requisição, se for POST(form submit) tenta logar.
if($this->request->is('post')) {
    if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash(__('Username ou password inválidos!'));
    }
}
    
28.02.2014 / 12:28