3

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.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
sradha
  • 2,216
  • 1
  • 28
  • 48

2 Answers2

4

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.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

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 ?!!

Fury
  • 4,643
  • 5
  • 50
  • 80