0

Just for organization sake, I wanted to use a different table for the authentication component to check, but it doesn't quite work. While I can initially state:

$this->Auth->userModel = "CoreUsers" plus set the loginAction to my proper MVC works to look at that table just to confirm it's there, but the login doesn't work, it only keeps returning an incorrect password. Something happens in the authentication component; I can't tell what makes it fail. When I rename my table to "Users", it works.

The other part is I'd prefer to actually use the column name of 'email' rather than 'username' since that's really what I'm using anyway.

I am just not having luck finding a complete tutorial and reference sets to do both these successfully with CakePHP 2.x. What is the way forward?

References:

  1. Stack Overflow question How do I use a table other than "Users" for CakePHP's AuthComponent?

  2. Stack Overflow question CakePHP - 'AuthComponent' with a different model name (not 'User')

(I had a look for answers, but I never quite got the whole answer.)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Emotinator
  • 13
  • 1
  • 3

2 Answers2

2

Make sure your database table "core_users" and model "CoreUser" exists.

When you setup component you can put login/logout redirect here.

var $components = array(
        "Auth" => array(
             'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
             'logoutRedirect' => array('controller' => 'core_users', 'action' => 'login')
              ),
       "Session");

Now in beforeFilter medhod you can put the following

function beforeFilter(){
      $this->Auth->authenticate = array(
         AuthComponent::ALL => array('userModel' => 'CoreUser', 'scope' => array("CoreUser.status" => 1), "fields" => array("username" => "email", "password" => "your_password_field"), 'Form', 'Basic'
                                        );
}

Above example you can ignore status, if you need to pass any other info on the login verification u can use that. Now about you can remove 'Basic' if you only need form validation. I hope this would work .

bdsarwar
  • 129
  • 1
  • 2
  • 9
1

First, model names are generally singular. Are you sure you didn't mean CoreUser which would look in the core_users table?

Secondly, you can use the email field instead of username by setting the fields key on your auth setup.

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'CoreUser',
                'fields' => array('username' => 'email')
            )
        )
    )
);

See the book for more information.

jeremyharris
  • 7,884
  • 22
  • 31
  • As for the model names, most of the examples and and CakePHP tutorial has the model name as singular. But I'll double check. – Emotinator Mar 29 '12 at 16:22
  • Oh man terrible typo on my part! Yes singular that's what I meant. Your post shows a plural one. – jeremyharris Mar 29 '12 at 16:24
  • I missed the highlight of the actual 'userModel' reference should have been singular, my tables were correctly named but that line I had wrong. Thanks. – Emotinator Mar 29 '12 at 17:10