Login Cakephp 2.0 does not work

Asked

Viewed 931 times

1

I had already successfully logged in with the default table and fields in cakephp, I decided to change the table and fields for login and now do not log in.

File Logs/login.ctp:

      <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 email and password.

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

  • 1

    You specified the name of the table in the model?

  • 1

    I think you’re calling the wrong model in your view

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 to the Model "User":

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

So where is

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

need to be

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

or

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

1

I had the same problem with version 2.4.5, had to leave the table as USER even though it is the default of CAKE. Just follow 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);
}

0

As for the problem of using other fields instead of cake patterns, I was able to solve this by configuring it directly in the Appcontroller beforeFilter:

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

Browser other questions tagged

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