1
votes

I have two type of users in my website. 1.User (Frontend) 2.Admin (Backend)

I used cakephp auth component for login both type of user. my problem is that when i login using one type of users i will automatically login to other type of user for example i login as user from frontend but when i refresh backed it will show me logedin and same problem with admin type of user.

Below are my auth Code separate for admin and user.

This is AppController.php file in app/Controller

public function beforeFilter() {
    if($this->Auth->user()){
    $this->set('logged_in', true);
    }else{
    $this->set('logged_in', false);
    }
    //Configure AuthComponent
    $this->Auth->userScope = array('User.is_active' => '1');
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'signin','plugin' => 'umgmt');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'signin','plugin' => 'umgmt');
    $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index','plugin' => false);

    }

This is my admin plugin controller code

public $components = array(
    'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'Admin',
                    'fields' => array(
                        'username' => 'username',
                        'password' => 'password'
                    ),
                     'scope'=>array('Admin.is_active' => 1)
                )
            )
        )
    );


public function beforeFilter() {

    $this->Auth->loginAction = array('controller' => 'admins', 'action' => 'index');
    $this->Auth->logoutRedirect = array('controller' => 'admins', 'action' => 'index');
    $this->Auth->loginRedirect = array('controller' => 'admins', 'action' => 'dashboard');

}

I am using cakephp 2.4.1

Please help me

1
where is login action ? - Moyed Ansari
Moyed Ansari i have update code above for admin login action - cakedev

1 Answers

0
votes

Okay. Firstly a quick note, you do not need to set your logged_in and out variables as the AuthComponent provides this functionality with the AuthComponent::loggedIn() method.

Secondly you have not set your type of Auth. As per the book, it's usually wise to use Controller so that you can manage your authentication on a per action basis.

So you need to update your component configuration or your beforeFilter() to include the type of authorization.

$this->Auth->authorize = array('Controller');

Then you can use the isAuthorized() method in your AppController to check the roles of your users based on the routing prefix being used.

All of this implementation is covered in the Cake book in the chapter on Auth.