Artisan
Artisan

Reputation: 4112

CakePHP 2 AuthComponent

I cannot login any users using AuthComponent. The user table's name is users, with some important fields such as user_id, user_password, there is no hashing on the password field.

This is my AppController

class AppController extends Controller {
  public $components = array(
    'Session',
    'Auth' => array(
      'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
      'logoutRedirect' => array('controller' => 'users', 'action' => 'home'),
      'authError' => 'You cannot view this page',
      'authorize' => array('controller')
    )
  );

  public function isAuthorize($user) {
    return true;
  }

  public function beforeFilter() {
    $this->Auth->allow('home');
  }
}

This is my UsersController

class UsersController extends AppController {
  public function login() {
    if ($this->request->is('post')) {
      if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
      } else {
        $this->Session->setFlash('Cannot login in');  
      }
    }
  }
}

This is my User model.

class User extends AppModel {
  public $name = 'User';
  public $primaryKey = 'user_id';
  public $belongsTo = 'Group'; 
}

This is my View

<h2>Login</h2>

<?php

echo $this->Form->create();
echo $this->Form->input('user_id', array('label' => 'User ID', 'type' => 'text'));
echo $this->Form->input('user_password', array('label' => 'Password', 'type' => 'password'));
echo $this->Form->end('Login');

?>

When I typed corrected user_id and password then pressed the Login button, I got the message from the UsersController that I cannot login. What went wrong here???

Also, I really don't understand about the concept of AuthComponent:login(), how does it work to check user_id and password againt the database, how doest it know which field conttains user_id, and which one contains the password???

Please help. Thanks. Kongthap

Upvotes: 0

Views: 926

Answers (1)

joshua.paling
joshua.paling

Reputation: 13952

A few things I noticed:

public function isAuthorize($user) {

This method is missing a 'd' on the end. It should be

public function isAuthorized($user) {

Next, by default, Cake expects to identify the user by fields named 'username' and 'password'. So, if you want to change that, you'll need to do this:

class AppController extends Controller {
  public $components = array(
    'Session',
    'Auth' => array(
      'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
      'logoutRedirect' => array('controller' => 'users', 'action' => 'home'),
      'authError' => 'You cannot view this page',
      'authorize' => array('controller'),
      'authenticate' => array(
          'Form' => array( // THIS IS WHERE YOU CHANGE THE DEFAULT FIELDS
             'fields' => array('username' => 'user_id','password' => 'user_password')
          )
       )
    )
  );

That code isn't tested but should set you on the right track. But as Dave said, it's really worth reading through the complete doco to understand how it all works: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html

Last, I'm not sure that 'user_id' is a good choice of column name. You'd expect a column name of 'user_id' to be a foreign key in some table, pointing to the 'id' column of a 'users' table. If that's not the function it serves, you should probably go with a different name.

Upvotes: 1

Related Questions