Question

I have problems with development of CakePHP2's authentication system, where users are stored in database table participants (not users, like usual).

It simply does not authenticate participant.

Table participants have next structure:

CREATE TABLE IF NOT EXISTS `participants` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `confirmed` tinyint(1) NOT NULL DEFAULT '0',
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `password` char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
  `token` char(32) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
)

File AppController.php have content like this:

App::uses('Controller', 'Controller');

class AppController extends Controller {
    public $components = array('Cookie', 'Session', 'Auth');
    function beforeFilter() {
        $this->Auth->userModel = 'Participant';
        $this->Auth->fields = array('username' => 'name', 'password' => 'password');
        $this->Auth->loginAction = array('controller' => 'participants', 'action' => 'login');
        //$this->Auth->logoutRedirect = array('controller' => 'participants', 'action' => 'logout'); // i will use this later
        //$this->Auth->loginRedirect = array('controller' => 'participants', 'action' => 'index');
    }
}

File ParticipantsController.php have content like:

App::uses('AppController', 'Controller');

    class ParticipantsController extends AppController {

        function beforeFilter() {
            parent::beforeFilter(); 
      $this->Auth->allowedActions = array('registration', 'login', 'forgotten', 'recreate', 'confirm');
        }

        function login() {

            if ($this->Auth->login()) {

            $this->redirect(array('controller' => 'participants', 'action'=>'view'));

        } else {
            // it always end-up here
            //pr($this->data);
            //pr(AuthComponent::password($this->data['Participant']['password']));
            //exit;

            $this->Session->setFlash( __('Error, please try again.', true) );

        }

    }

I don't know what is wrong here, can you please help me what I'm missing here?

Was it helpful?

Solution

I think it might be your configuration of fields and userModel

$this->Auth->fields

My working code is closer to :

$this->Auth->authenticate = array(
    'Form' => array('userModel' => 'Participant'
                   , 'fields' => array('username' => 'name', 'password' => 'password')
                   )
);

OTHER TIPS

Try this:

   public function beforeFilter() {
         parent::beforeFilter(); 
         $this->Auth->allow(array('registration', 'login', 
                'forgotten', 'recreate', 'confirm'));
    }

Why your functions is not public in controllers?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top