3
votes

i am developing with cakephp 2.4.7 where i am using the auth component for multiple login (a user and a company login).

My goal is to set the right sessionKey (Auth.User or Auth.Company) in the beforeFilter. Auth.User is the default value in cakephp.

AppController:

public $helpers = array('Cache','Html','Session','Form');

public $components = array(
    'Security',
    'Cookie',
    'RequestHandler',
    'DebugKit.Toolbar',
    'Session',
    'Auth' => array(
        'loginRedirect' => array(
            'controller' => 'users',
            'action' => 'index'
        ),
        'logoutRedirect' => array(
            'controller' => 'users',
            'action' => 'login'
        ),
        'authError' => 'You must be loggedin to view this page.',
        'loginError' => 'Invalid user credentials.',
        'authorize' => array('Controller')
    )
);

public function beforeFilter() {

    $this->Auth->deny('*'); 
}

CompaniesController:

public function beforeFilter() {
    parent::beforeFilter();
    AuthComponent::$sessionKey = 'Company';
    //$this->Auth->sessionKey = 'Auth.Company';
    $this->Auth->authenticate = array(
        'Form' => array(
            'userModel' => 'Company', // set the new userModel
            'fields' => array('username' => 'email')
        )
    );
    $this->Auth->allow('register', 'login', 'logout');
}

The login works perfectly, but the auth-session is still Auth.User. (Tested with debug($this->Auth->User());)

What i am doing wrong? How can i set the AuthComponent::$sessionKey correctly?

1
Are you sure $this->Auth->user() isn't reading from the Company key in session? What happens if you try to debug the whole of $this->Session->read() or just $this->Session->read('Company)? Apologies if that's a stupid question, but thought you might as try it out. - SharkofMirkwood
$this->Auth->user() returns the array set under the specified session key. So you really can't check the out of that method to know under what session key the info is stored. Does your Company model has belongsTo or hasOne association with User model? - ADmad
No, company has no association to the User model. - q0re
debug(CakeSession::read('Auth')); returns the company array. - q0re
@q0re are you using two different tables to authenticate the user or only one table? and if so, how do you redirect them to the there respective dashboards after successful login? please help as I am new to cakephp. - Sagar Guhe

1 Answers

2
votes

I had a same problem today, I have jumped into code (here) to check why its not working for me.

It seems that you have to set it like this

public function beforeFilter()
{

    AuthComponent::$sessionKey = 'Auth.Company'; // static property so we have to 
    // access in static way so you want get strict errors
    ...
}

and then logout and login user again. In your action just var_dump() or pr() the $this->Session->read('Auth')

Btw $this->Auth->user() will always return you array that is in the Auth by [$sessionKey] and its same for AuthComponent::user() static call.