# Cakephp 3 - Auth Login Return False

**URL:** <https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939>\
**Category:** Need Help\
**Created:** [July 28, 2017, 2:42am UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939 "2017-07-28T02:42:15Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabinhokls](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/fabinhokls/32/733_2.png) [@fabinhokls](https://discourse.cakephp.org/u/fabinhokls)\
**Post date:** [July 28, 2017, 2:42am UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/1 "2017-07-28T02:42:15Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![littleylv](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/littleylv/32/494_2.png) [@littleylv](https://discourse.cakephp.org/u/littleylv)\
**Post date:** [July 28, 2017, 2:58am UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/2 "2017-07-28T02:58:04Z")

</div>

> [@fabinhokls](#):
>
> ‘authenticate’ =\> [  
> ‘Form’ =\> [  
> ‘fields’ =\> [‘email’ =\> ‘email’, ‘senha’ =\> ‘senha’],  
> ‘userModel’ =\> ‘Conta’,  
> ‘finder’ =\> ‘auth’,  
> ]  
> ],

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

```

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

---

<div class="post-metadata">

**Author:** ![fabinhokls](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/fabinhokls/32/733_2.png) [@fabinhokls](https://discourse.cakephp.org/u/fabinhokls)\
**Post date:** [July 28, 2017, 3:06am UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/3 "2017-07-28T03:06:47Z")

</div>

It was the first thing I tried to do!

---

<div class="post-metadata">

**Author:** ![littleylv](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/littleylv/32/494_2.png) [@littleylv](https://discourse.cakephp.org/u/littleylv)\
**Post date:** [July 28, 2017, 3:12am UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/4 "2017-07-28T03:12:49Z")

</div>

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

so just

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

```

---

<div class="post-metadata">

**Author:** ![fabinhokls](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/fabinhokls/32/733_2.png) [@fabinhokls](https://discourse.cakephp.org/u/fabinhokls)\
**Post date:** [July 28, 2017, 3:15am UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/5 "2017-07-28T03:15:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![littleylv](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/littleylv/32/494_2.png) [@littleylv](https://discourse.cakephp.org/u/littleylv)\
**Post date:** [July 28, 2017, 3:20am UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/6 "2017-07-28T03:20:54Z")

</div>

by using

```auto
        '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?

---

<div class="post-metadata">

**Author:** ![fabinhokls](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/fabinhokls/32/733_2.png) [@fabinhokls](https://discourse.cakephp.org/u/fabinhokls)\
**Post date:** [July 28, 2017, 3:25am UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/7 "2017-07-28T03:25:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![littleylv](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/littleylv/32/494_2.png) [@littleylv](https://discourse.cakephp.org/u/littleylv)\
**Post date:** [July 28, 2017, 3:28am UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/8 "2017-07-28T03:28:15Z")

</div>

use the second code please

---

<div class="post-metadata">

**Author:** ![fabinhokls](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/fabinhokls/32/733_2.png) [@fabinhokls](https://discourse.cakephp.org/u/fabinhokls)\
**Post date:** [July 28, 2017, 8:22pm UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/9 "2017-07-28T20:22:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![frankhdesign](https://avatars.discourse-cdn.com/v4/letter/f/ecccb3/32.png) [@frankhdesign](https://discourse.cakephp.org/u/frankhdesign)\
**Post date:** [May 29, 2018, 4:43pm UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/10 "2018-05-29T16:43:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [May 29, 2018, 10:25pm UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/11 "2018-05-29T22:25:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![frankhdesign](https://avatars.discourse-cdn.com/v4/letter/f/ecccb3/32.png) [@frankhdesign](https://discourse.cakephp.org/u/frankhdesign)\
**Post date:** [July 3, 2018, 8:28pm UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/12 "2018-07-03T20:28:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![phucph](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@phucph](https://discourse.cakephp.org/u/phucph)\
**Post date:** [May 11, 2019, 2:45pm UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/13 "2019-05-11T14:45:51Z")

</div>

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’]);

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 11, 2019, 8:59pm UTC](https://discourse.cakephp.org/t/cakephp-3-auth-login-return-false/2939/14 "2019-05-11T20:59:58Z")

</div>

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