Search code examples
cakephpauthenticationcakephp-3.0identify

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!


Solution

  • Change 'fields' => ['email' => 'email', 'senha' => 'senha'] to 'fields' => ['username' => 'email', 'password' => 'senha']

    With this config, you don't need a custom finder, unless you require to select columns. By using the default finder, you could always unset properties.

    Make sure your login form controls and table columns are named email and senha

    Then, I'm wondering if you eighter way should load authcomponent in AppController.