Login CakePHP 2.0 does not work

1

I have successfully made the login system with the table and fields that come by default in cakephp, I have decided to change the table and fields to login and now does not log in.

Logs / Login.ctp file:

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

AppController file:

    class AppController extends Controller {


     public $components = array(
      'Session',
      'Auth' => array(
    'authenticate' => array(
        'Form' => array(
            'userModel' => 'Registo',
            'fields' => array(
                'username' => 'email',
                'password' => 'password'
            )
        )
    ),
    'loginAction' => array('controller' => 'registos', 'action' => 'login'), 
    'loginRedirect' => array('controller' => 'anuncios', 'action' => 'index')
     )
    );

 function beforeFilter() {

    $this->Auth->allow();
}

 }

Action Login:

    public function login() {

       if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
       } else {
        //$this->Session->setFlash(__('Invalid username or password, try again'));
      }
    }

My table in the database is called users and has the email and the password.

It arrives to action login with the values of the correct email and password, but it does not login, I suspect that it is not checking in the right table, but I do not understand why.

    
asked by anonymous 27.02.2014 / 15:58

3 answers

1

I noticed 2 errors in your code

1 - If the table that stores your users is called "users" you must create the Form for the Model

2 - If the table you want is actually "records" then the Form should be created using the model name in the singular "

So where do you have

<?php echo $this->Form->create('Registos'); ?>

dferia ser

<?php echo $this->Form->create('Utilizador'); ?>

or

<?php echo $this->Form->create('Registro'); ?>
    
27.02.2014 / 19:48
1

I had the same problem with version 2.4.5, I had to leave the table as USER even though it is the CAKE standard. Here is my code:

AppController

class AppController extends Controller {


public $components = array(
    'Session',
        'Auth' => array(
        'loginRedirect' => array('controller' => 'painels', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'authenticate' => array(
        'Form' => array(
            'fields' => array('username' => 'email')
        )
    )

    ));


public function beforeFilter() {
       $this->Auth->allow();
    }

public function isAuthorized($user) {   
    return true;
}   

}

UsersController

  public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('login', 'add');

}

public function login() {
    $this->layout = 'home';

    //if already logged-in, redirect
    if ($this->Session->check('Auth.User')) {
        $this->redirect(array('controller' => 'painels', 'action' => 'index'));
    }


    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Session->setFlash(__('Login ou senha invalida.'));
        }
    }
}

public function logout() {
    $this->redirect($this->Auth->logout());
}

Model User

public function beforeSave($options = array()) {

    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }

    if (isset($this->data[$this->alias]['password_update'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password_update']);
    }


    // fallback to our parent
    return parent::beforeSave($options);
}
    
27.02.2014 / 21:25
0

As for the problem of using other fields instead of cake patterns, I was able to resolve this by configuring directly in AppController's beforeFilter:

          $this->Auth->authenticate = array(
            AuthComponent::ALL => array(
                'userModel' => 'Usuario',
                'fields' => array(
                    'username' => 'email',
                    'password' => 'senha'
                    ),
                'scope' => array(
                    'User.status' => 1,
                    ),
                ),
            'Form',
        );
    
28.03.2014 / 04:47