How do I use a table other than "Users" for CakePHP's AuthComponent?

Fahd picture Fahd · Jan 13, 2009 · Viewed 7.2k times · Source

CakePHP's AuthComponent assumes you have a Users table that contains a username and password. I'd like to find a way to override the default tablename from Users to Accounts.

Background Information:

The way I have designed my database is to have a Users table and an Accounts table.

Accounts:

  • id

  • user_id

  • username

  • password

  • authentication service (for example, my site, Facebook, Google, OpenID, etc.)

Users:

  • simply has all the personal information of the user (age, gender, etc.)

The reason for this is so that

  1. each user can have multiple accounts they can login from so they are not locked into one
  2. I can connect the third-party services to an account for more awesomeness

Now back to the problem....

CakePHP has documentation on changing the default field name, but I can't find anything on changing the default table name, but assume it would be similar in nature...

Example of changing the default field name:

function beforeFilter() {
    $this->Auth->fields = array(
            'username' => 'username',
            'password' => 'secretword'
        );
 }

Is there a way to accomplish this or should I restructure the tables keeping with CakePHP convention and still accomplish the same thing?

Answer

Fahd picture Fahd · Jan 13, 2009

In app_controller.php:

function beforeFilter() {
    $this->Auth->userModel = 'Account';
}