0
votes

I can't login using Auth in cakephp. There are some similar posts as this one on stackoverflow, however those answers seem not to work for me.

I have create a similar register form and that works with Auth but with logging in, $this->Auth->login(); returns false in the UsersController.

Auth uses the correct userModel and the username field is changed to email. (note: when I use username instead of email everywhere, it doesn't work either). The database has a table user that holds an email and a password field. The password field is hashed/encrypted with use of: AuthComponent::password()

// AppController

class AppController extends Controller {

    public $helpers = array('Html', 'Form');
    public $components = array(
        'Session',
        'Auth');

    public function beforeFilter() {
        $this->Auth->userModel = 'User';
        $this->Auth->fields = array('username' => 'email', 'password' => 'password');
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
    }
}

// UsersController

class UsersController extends AppController{
  var $name = 'Users';
  var $helpers = array('Html', 'Form');

  function beforeFilter()
  {       
    parent::beforeFilter();
    // tell Auth not to check authentication when doing the 'register' action
    $this->Auth->allow('register');
  }

  function login(){
      if (isset($this->data['User'])) {
        $this->Auth->login();
        debug('tried');
      }
  }

// login.ctp

<?php
    echo $this->Form->create('User',array('action'=>'login'));
    echo $this->Form->input('email');
    echo $this->Form->input('password');
    echo $this->Form->end('Login');  

?>

// user.php

<?php
class User extends AppModel{

    var $name = 'User';
}
?>

I'm only using cakephp for a few days so it is probably an easy error, however I'm searching for it for a few hours now and I have still not found it.

1
Which CakePHP version are you using? - ADmad

1 Answers

1
votes

Settings properties like $this->Auth->userModel and $this->Auth->fields won't do you any good in 2.x. You need to use $this->Auth->authenticate property to specify the userModel and fields options. Read the 2.x manual regarding Auth configuration for more info.