Thursday, November 1, 2012

Multiple login types on CakePHP 2

The Authentication component has been rewritten on CakePHP 2. I think it's not as simple as the previous version but I guess it's better going forward since there are multiple ways of authentications nowadays especially when creating API's.

If you need to create multiple login types on the same website, it is actually simple. You may need this in case you have 2 separate tables for 2 different types of users accessing your website. One way to do this is to use a different session key for each login type:

if ($loginTypeOne) {
  $this->Auth->authenticate = array(
    'Form'=> array(
      'userModel'=> 'TypeOne',
      )
    );
  AuthComponent::$sessionKey = 'Auth.TypeOne';
} else {
  $this->Auth->authenticate = array(
    'Form'=> array(
      'userModel'=> 'TypeTwo',
      )
    );
  AuthComponent::$sessionKey = 'Auth.TypeTwo';  
}

I also posted this on this thread on StackOverflow.

No comments:

Post a Comment