When I’m trying to achieve the following:
- User changes email
- User gets verification mail
- 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.
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:
Or re-authenticate the user, which should prompt a refresh (not 100% sure off the top of my head if this really works though):