sradha
sradha

Reputation: 2244

$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.

Upvotes: 3

Views: 3300

Answers (2)

Fury
Fury

Reputation: 4776

I came across the same problem. I am using Cake 3.7. This is for who use routes.

I created route in my routes file for login.

$routes->connect('/login',
        ['controller' => 'Users', 'action' => 'login'],
        ['_name' => 'login']
    );

Now when you use below code the Auth doesn't log you in and identify() returns false.

$this->loadComponent('Auth', [
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'loginRedirect' => ['what ... ever'],
        'logoutRedirect' => '/',
        'authError' => 'Please log in to view your account.',
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email']
            ]
        ],
    ]);

So ... you see... You have to call the route name for login attempt.

$this->loadComponent('Auth', [
        'loginAction' => ['_name' => 'login'], // <= Here is Mr glitch 
        'loginRedirect' => ['what ... ever'],
        'logoutRedirect' => '/',
        'authError' => 'Please log in to view your account.',
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email']
            ]
        ],
    ]);

Really ?!!

Upvotes: 0

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

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.

Upvotes: 4

Related Questions