Cakephp 3 - Auth session cannot destroy

允我心安 提交于 2019-12-25 09:15:41

问题


I use Cakephp 3.2.11 on Cloud 9 IDE server.

  1. When I logged out to my app via Auth component. I didn't log in again but I tried to access some pages. It was appeared Auth session login request like: (I didn't design it)

I type username & password in my Users table in database. It was LOGGED IN.

  1. Now when I tried log out, destroy all session; my app still recorded the session what I logged in as above. I use debug to check:

    debug($this->request->session()->read('Auth'));

Here my logout()

public function logout()
    {
        $this->request->session()->destroy();
        return $this->redirect($this->Auth->logout());
    }

My AppController.php with Auth component config

$this->loadComponent('Auth', [
            'authenticate' => array(
                'Form' => array(
                    // 'fields' => array('username' => 'email'),
                    'scope' => array('is_delete' => '0')
                )
            ),
            'loginAction' => [
                'controller' => 'MUsers',
                'action' => 'login'            
            ],
            'authorize' => ['Controller'],
            'loginRedirect' => [
                'controller' => 'Pages',
                'action' => 'dashboard'
            ],
            'logoutRedirect' => [
                'controller' => 'MUsers',
                'action' => 'login'
            ],
            'storage' => 'Session',
            'authError' => 'Woopsie, you are not authorized to access this area.',
            'flash' => [
                'params' => [
                    'class' => 'alert alert-danger alert-dismissible text-c',
                            ]
                        ]

Now I cannot delete that session using code, I just can delete it by clear the browser cache. So my questions are:

How can I solve this problem using code or config my app settings?

UPDATE

Based on @Kamlesh Gupta answered, it edited my code and it's ok.

$this->loadComponent('Auth', [
            'authenticate' => array(
                'Form' => array(
                'userModel' => 'MUsers', //Add this line
                'fields' => array('username' => 'username',
                                   'password' => 'password'), //Edited this line
                    'scope' => array('is_delete' => '0')
                )
            ),
            'loginAction' => [
                'controller' => 'MUsers',
                'action' => 'login'            
            ],
            'authorize' => ['Controller'],
            'loginRedirect' => [
                'controller' => 'Pages',
                'action' => 'dashboard'
            ],
            'logoutRedirect' => [
                'controller' => 'MUsers',
                'action' => 'login'
            ],
            'storage' => 'Session',
            'authError' => 'Woopsie, you are not authorized to access this area.',
            'flash' => [
                'params' => [
                    'class' => 'alert alert-danger alert-dismissible text-c',
                            ]
                        ]

回答1:


For login authentication,

Use below code in appController.php

$this->loadComponent('Auth', [
             'authenticate' => [
                 'Form' => [
                     'userModel' => 'Users',
                     'fields' => array(
                         'username' => 'email',
                         'password' => 'password'
                     ),
                 ],
             ],
            'logoutRedirect' => [
                    'controller' => 'users',
                    'action' => 'login'
                ],
             'loginAction' => [
                 'controller' => 'Users',
                 'action' => 'login'
             ],
             'unauthorizedRedirect' => false,
             'storage' => 'Session'
         ]);

**for destroying session** 
public function logout()
{
  $this->Auth->logout();
}

This code is work for me. i am using in my app.

you can also try just changing model name and fieldname, action



来源:https://stackoverflow.com/questions/39074881/cakephp-3-auth-session-cannot-destroy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!