2

I am not able to get user details as $this->Auth->identify(); returns false. I am not using DefaultPasswordHasher. Instead i am using LegacyPasswordHasher.

Below code is for LegacyPasswordHasher:

<?php     
namespace App\Auth;

use Cake\Auth\AbstractPasswordHasher;
use Cake\Utility\Security;    

class LegacyPasswordHasher extends AbstractPasswordHasher
{

    public static $hashType = null;

    public function hash($string, $type = null, $salt = false) {
        if (empty($type)) {
            $type = static::$hashType;
        }
        $type = strtolower($type);

        if ($type === 'blowfish') {
            return static::_crypt($string, $salt);
        }
        if ($salt) {
            if (!is_string($salt)) {
                $salt = Security::salt();
            }
            $string = $salt . $string;
        }

        if (!$type || $type === 'sha1') {
            if (function_exists('sha1')) {
                return sha1($string);
            }
            $type = 'sha256';
        }

        if ($type === 'sha256' && function_exists('mhash')) {
            return bin2hex(mhash(MHASH_SHA256, $string));
        }

        if (function_exists('hash')) {
            return hash($type, $string);
        }
        return md5($string);
    }

       public function check($password, $hashedPassword)
    {
        return $this->hash($password) === $hashedPassword;
    }

}

Below is the code for _setPassword method in User entity:

protected function _setPassword($password)
{
    if (strlen($password) > 0) {
        return (new LegacyPasswordHasher)->hash($password,null,true);
    }
}

And this is my initialize() in AppController:

    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'passwordHasher' => [
                    'className' => 'Legacy',
                ]
            ]
        ]
    ]);
}

And this is my login() in UsersControllers which is returning false:

public function login() {

    if($this->request->is('post')) {
        $user = $this->Auth->identify();
        var_dump($user);
        exit();
    }

}
tereško
  • 58,060
  • 25
  • 98
  • 150
Varuni N R
  • 802
  • 3
  • 11
  • 31

0 Answers0