phpcakephpcakephp-1.3

Auto login after registration in CakePHP 1.3


I create a new User with this code: controller:

$this->User->createUser($this->data)

Model:

function createUser(){ 
    $this->create();
}

After that I want to login the user. I already tryed this (in the controller):

$this->Auth->login($this->data);
$this->redirect('home');

Unfortunately it does not work that way. Am I doing something wrong?


Solution

  • For cake1.3

    In your controller

    $id = $this->User->createUser($this->data);
    $this->data['User'] = array_merge($this->data['User'], array('id' => $id));
    $this->Auth->login($this->data);
    $this->redirect('home');
    

    Model

    before creating user you've to hash the user entered password and then save into the database

    function createUser($data){ 
        $data['User']['password'] = md5($data['User']['password']);
        $this->save();
        return $this->id; // return id of last saved record
    }