
Cakephpを利用したログイン機能実装にはAuthComponentを利用する場合は多いと思います。
Cakephp2からCakephp4へのアップデートの際、AuthComponent::login() はCake3以降からは削除されています。
その代替えとしてAuthComponent::identify() を使用することになります。
Cakephp2: AuthComponent::login()
// 多分、こんな使い方が多いと思われる
if (!$this->Auth->login()) {
// ログイン失敗時の処理
}
Cakephp4: AuthComponent::identify()
public function login()
{
if ($this->request->is('post')) {
// POSTリクエストに対し、認証を行う
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__('Username or password is incorrect'));
}
}
}
identify()は、AppControllerなどでAuth設定したfieldsに対し、POSTリクエストを受け取って認証を行います。
また、Cakeアップデートの場合では、Cakephp2の古いハッシュ方式に認証を対応させる必要が出てきます。
public function initialize(): void
{
parent::initialize();
$authenticateConfig = [
'Form' => [
'userModel' => 'Hoges',
'fields' => ['username' => 'email', 'password' => 'password'],
'passwordHasher' => [
'className' => 'Fallback',
'hashers' => [
'Default',
'Weak' => ['hashType' => 'sha1'], // Cakephp2のhashTypeに対応
],
],
],
];
$this->loadComponent('Auth', [
'authenticate' => $authenticateConfig
]);
}
■ Cakephp2 AuthComponent::login()
https://api.cakephp.org/2.10/class-AuthComponent.html#_login
■ Cakephp4 AuthComponent::identify()とマニュアル
https://api.cakephp.org/4.2/class-Cake.Controller.Component.AuthComponent.html#identify()
https://book.cakephp.org/4/ja/controllers/components/authentication.html#id6