Cakephp 3 - Auth Login Return False

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!

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

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

It was the first thing I tried to do!

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

so just

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

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!

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?

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();

use the second code please

1 Like

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.

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.

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

It was set to that. I fixed it, but now I can’t remember what I did to fix it. I forgot to update this for others that may be having the same issue. If I remember I will be sure to udate it.

Hey !
Please check if I have added the router for authentic work.
Try adding router.php
routes-> connect ('/', ['controller' => 'users', 'action' => 'login']); routes-> connect (’/’, [‘controller’ => ‘users’, ‘action’ => ‘logout’]);

@phucph, this is a completely unrelated question. You should post it as a new one, not as a follow-up here.