PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] CakePHP Auth->identify() returning false

 March 18, 2022     cakephp, cakephp-3.0     No comments   

Issue

I'm playing around with CakePHP and can't seem to get the login working. It seems that $this->Auth->identify() is constantly returning false and not allowing me to login.

I've read all previous posts regarding my issue, however, none have provided me with a solution. The users I am trying to log in as have all been created using the cake password hasher, which I have checked have been stored in the database. I have checked the password field length in the database, which is set to varchar (255), I've checked the Auth => authenticate => Form => fields are set to the correct values (the login.ctp fields are also correct). I also tried changing the $this->Form->control() to $this->Form->input() as someone suggested, with no luck.

AppController:

 $this->loadComponent('Auth', [
            'loginRedirect' => [
                'controller' => 'Users',
                'action' => 'classes'
            ],
            'logoutRedirect' => [
                'controller' => 'Users',
                'action' => 'index'
            ]
  ]);
$this->loadComponent('Auth', [
                'authenticate' => [
                    'Form' => [
                        'fields' => [
                            'username' => 'email',
                            'password' => 'password'
                        ]
                    ]
                ],
                'loginAction' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ]
        ]);

login() function in UsersController:

public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            pj($user);
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect(['controller' => 'users']);
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }

login.ctp:

<div class="users form">
<?= $this->Form->create() ?>
    <fieldset>
        <legend><?= __('Please enter your username and password') ?></legend>
        <?= $this->Form->input('email') ?>
        <?= $this->Form->input('password') ?>
    </fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>

EDIT: I forgot to add that I can successfully add users, I just can't log them in.


Solution

You are loading the AuthComponent twice in AppController. The second load with the config for form fields is not loaded.

Load the component one time with the wanted config.

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'fields' => [
                'username' => 'email',
                'password' => 'password'
            ]
        ]
    ],
    'loginRedirect' => [
        'controller' => 'Users',
        'action' => 'classes'
    ],
    'logoutRedirect' => [
        'controller' => 'Users',
        'action' => 'index'
    ],
    'loginAction' => [
        'controller' => 'Users',
        'action' => 'login'
    ]
]);


Answered By - Christian
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing