Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$this->Auth->identify(); returning false in cakephp 3.2

I have done all the settings for login in cakephp 3.2,but while login it is returning false.

login function in usercontroller

  public function login() {
        $this->viewBuilder()->layout('');
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            pj($user);//returning false
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }

This function $user = $this->Auth->identify(); is always returning false. Why can't i login? In data base password is stored as

$2y$10$4aD6Ye6YcmPGKgI/CmhJBO0E//9tV.KvhJIOFAhajyqt8vfxDVASC

I am getting the email and password from $this->request->data.

Any suggestion will highly appreciate. thank you in advance.

like image 636
sradha Avatar asked Feb 19 '26 13:02

sradha


1 Answers

Please change like below:-

First in your controller add this code (best will be App controller):-

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Auth', [
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login',
            'plugin' => 'Users'
        ],
        'authError' => 'Did you really think you are allowed to see that?',
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email']
            ]
        ],
        'storage' => 'Session'
    ]);
}

And then use your code. It will be work properly.

like image 53
Anant Kumar Singh Avatar answered Feb 21 '26 01:02

Anant Kumar Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!