Timo Willemsen
Timo Willemsen

Reputation: 8857

CakePHP Auth component doesn't reflect changes on user data

When I'm trying to achieve the following:

  1. User changes email
  2. User gets verification mail
  3. User activates changed email.

The way I do this is the following:

<?php
echo $form->create('User', array('action' => 'changeEmail'));
echo $form->input('email');
echo $form->end('Change');
?>

Then my controller

function changeEmail(){
 if(!empty($this->data)){
  $user = $this->Auth->user();
  $user['User']['email'] = $this->data['User']['email'];
  $activationcode = _generateActivationCode();
  $user['User']['activationcode'] = $activationcode;
  $user['User']['isactive'] = false;

  if($this->User->save($user)){
    //sendmail
  }
 }

Then I get an view where they can activate the email, and I do the following:

function activate(){
 $user = $this->Auth->user();

 //Now this variable doesn't reflect the changes we've just saved (for example the activationCode)
}

Is this intended behavior or am I doing something wrong here.

Upvotes: 2

Views: 1091

Answers (1)

deceze
deceze

Reputation: 521995

The AuthComponent caches the user data in the session. This data is not refreshed until the user logs in again. You can overwrite the data in the session explicitly:

$this->Session->write('Auth.User', $user);

Or re-authenticate the user, which should prompt a refresh (not 100% sure off the top of my head if this really works though):

$this->Auth->login($this->Auth->user('id'));

Upvotes: 6

Related Questions