Cakephp 3 - Auth Login Return False


#1

What’s the problem with this code? The Controller and Identify only return false! My database column is senha (password) and email. And I can not sign in. I am using hash, password with 255 characters and all right. But it’s not working!

ContaController.php

public function initialize() {
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => ['email' => 'email', 'senha' => 'senha'],
                'userModel' => 'Conta',
                'finder' => 'auth',
            ]
        ],
        'authorize' => ['Controller'],
        'loginAction' => [
            'controller' => 'Conta',
            'action' => 'index',
        ],
        'loginRedirect' => [
            'controller' => 'Conta',
            'action' => 'minha-agenda'
        ],
        'logoutRedirect' => [
            'controller' => 'Conta',
            'action' => 'index',
        ],
        'storage' => 'Memory'
    ]);
    $this->Auth->allow(['index']);
}


public function index() {
    if ($this->request->is('ajax') || $this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            // return $this->redirect($this->Auth->redirectUrl());
            echo 'success';
        } else {
            var_dump($user);
            echo 'incorrect';

        }
    }
}

ContaTable.php

public function initialize(array $config) {
    parent::initialize($config);

    $this->setTable('alunos');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsToMany('Alunos', [
        'foreignKey' => 'interesses_id',
        'targetForeignKey' => 'alunos_id',
        'joinTable' => 'alunos_interesses'
    ]);
}

public function validationDefault(Validator $validator) {
    $validator
        ->notEmpty('email', 'A username is required')
        ->notEmpty('senha', 'A password is required');
    return $validator;
}

public function findAuth(\Cake\ORM\Query $query, array $options) {
    $query
        ->select(['id', 'email', 'senha'])
        ->where(['Conta.email' => $options['email']])
        ->andWhere(['Conta.senha' => $options['senha']]);
    return $query;
}

I need help solving this problem. The columns in the database are different, so I do not intend to use it as default. More ahead it will become ajax but for now it is so because I could not solve!


#2
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email', 'password' => 'senha'],
                'userModel' => 'Conta',
                'finder' => 'auth',
            ]
        ],

you should not change the key ‘username’ and ‘password’


#3

It was the first thing I tried to do!


#4

and 'finder' => 'auth', is not needed.

so just

        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email', 'password' => 'senha'],
                'userModel' => 'Conta'
            ]
        ],

#5

I need this because my table is with the columns are email and senha and in the findAuth method I set it up! It does not make a mistake, but it does not work, I’ve tried it anyway! And keep trying!


#6

by using

        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email', 'password' => 'senha'],
                'userModel' => 'Conta'
            ]
        ],

you have already changed your columns to email and senha. and you dont need ‘finder’ => ‘auth’ any more.

try my code, won’t you?


#7

But in html I do this …

echo $this->Form->create('Conta'); 
echo $this->Form->email('username'); 
echo $this->Form->password('password'); 
echo $this->Form->submit('Entrar'); 
echo $this->Form->end(); 

Or do I do so?

echo $this->Form->create('Conta'); 
echo $this->Form->email('email'); 
echo $this->Form->password('senha'); 
echo $this->Form->submit('Entrar'); 
echo $this->Form->end();

#8

use the second code please


#9

It worked perfectly, but now it is giving a mistake. It passes through the identify and returns the data but when it arrives in setUser it returns Null.


#10

This is old but I am having the same problem. The user is always returning null. I created the table from the tutorial script and followed all the instructions and still no go. I had this issue on another app with an earlier version fo cakephp3 and got it to work. It seems Auth has changes and those methods do not work anymore.


#11

@frankhdesign check your password field width in your database, it should be IIRC >= 64 thats the most common cause