LoginSignup
2
5

More than 5 years have passed since last update.

Cakephp3のAuthComponent

Last updated at Posted at 2017-06-01

備忘録
タイトル通りCakephp3のAuthComponentを使ってみた

読み込み

AppController.php
public function initialize() {
    parent::initialize();
    .
    .
    .
        $this->loadComponent('Auth', array(
            'authenticate' => array(
                'Form' => array(
                    'fields' => array(
                        'username' => 'user_id', // ユーザIDに該当するもの
                        'password' => 'pass' // パスワード
                    )
                )
            ),
            // 認証するアクション
            'loginAction' => array(
                'controller' => 'Users',
                'action' => 'login',
                'prefix' => false
            ),
            // ログイン成功時のリダイレクト先
            'loginRedirect' => array(
                'controller' => 'Dashboards',
                'action' => 'index',
                'prefix' => false
            ),
            // ログアウト後のリダイレクト先
            'logoutRedirect' => array(
                'controller' => 'Users',
                'action' => 'login',
                'prefix' => false
            ),
            'authError' => 'アクセス権限がありません' // アクセス制御した時のメッセージ
        ));
}

ログアウト用設定

ログアウトするアクションへの許可を書いておかないと
セッション切れ => ログアウトボタンおす => 'アクセス権限がありません'
ってなったので書いておく

UsersController.php
public function initialize() {
    parent::initialize();
    .
    .
    .
    $this->Auth->allow('logout');
}

参考

git : https://github.com/cakephp/cakephp/blob/master/src/Controller/Component/AuthComponent.php
公式 : https://book.cakephp.org/3.0/ja/controllers/components/authentication.html

2
5
3

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
2
5