LoginSignup
4
1

More than 1 year has passed since last update.

Cakephp4 Authentication プラグイン ログインできない (login URL did not match)

Last updated at Posted at 2021-06-10

環境

  • CakePHP4.2.6
  • PHP7.4.16
  • cakephp/authentication2.6.1

やりたかったこと

公式の認証チュートリアルを参考に、Usersテーブルを使って、src/Controller 直下にある UsersController 経由でログインする。ただこれだけ。

時系列:やったこと

ログイン機能の作成

認証チュートリアルに書いてある通り実装。

ログイン画面にリダイレクトされない

ログイン画面にリダイレクトされるか確認するとなぜか、開発サーバのルートに飛んでしまいログインページが表示されない。

Router::urlを使用して回避

こちらの記事を参考に以下のように Router を使用してリダイレクトを指定。

use Cake\Routing\Router;

$authenticationService = new AuthenticationService([
            'unauthenticatedRedirect' =>  Router::url(['controller' => 'Users', 'action' => 'login']),
            'queryParam' => 'redirect',
        ]);

これでログイン画面にリダイレクトされた。

今度はログインできない

ログイン画面にリダイレクトされると思ったら、次はログインできない。
hashの確認したが特に問題はなさそう・・・。
print_r($result); で確認すると、

Authentication\Authenticator\Result Object ( [_status:protected] => FAILURE_OTHER [_data:protected] => [_errors:protected] => Array ( [0] => Login URL `/hogehoge/users/login` did not match `/users/login`. ) )

LoginURL did not matchで調べたところこちらの記事を見つけたので、書いてある通りに loginURL も Router を使うように変更。

こちらもRouter::urlを使用して回避

$authenticationService->loadAuthenticator('Authentication.Form', [
            'fields' => [
                'username' => 'email',
                'password' => 'password',
            ],
            'loginUrl' => Router::url([
                'plugin' => null,
                'prefix' => null,
                'controller' => 'Users',
                'action' => 'login',
            ]),
        ]);

無事ログインできた。
誰かの役に立ちますように・・・。

参考

【CakePHP4】Authentication plugin:サブディレクトリに設置したCake4で、未ログイン時のリダイレクト先が荒ぶる【Router使おう】
CakePHP 4 : CakePHP Route Url Missing Route
Cakephp 3 Authentication plugin, login URL did not match

4
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1