Cannot update email of logged in user

Hello there,

I want to let the logged in user update his or her email by a form submission in the front-end. But unfortunately the email address does not change. This is my controller:

<?php

return function ($kirby, $page) {

  $error = false;

  if ($kirby->request()->is('POST') && get('submit-mail-change')) {

    try {
      $user = $kirby->user();
      $data = $kirby->request()->data();

      $kirby->impersonate('kirby');
      $user->changeEmail($data['new-email']);

    } catch (Exception $e) {
      $error = true;
    }

    if ($error == false) {
      go($page->url());
    }

  }

};

If I submit the form, I get no exception, and after reload the email returned by $kirby->user()->email() is kirby@getkirby.com. After another reload, the returned value is the old email address from before the change.

Does anybody see the problem in my code or know where the problem may be?

Thank you very much in advance!

Sigi

If a user is logged in, you don’t want to use an impersonated user.

Oh, yes, thank you for pointing that out! But after deleting the line in my code, the email address still does not get updated. Do you see anything else problematic?

Doesn’t get updated in the user content file or doesn’t get updated on the frontend?

Neither in the content file, nor on the frontend.

I am using a composer setup with Kirby v3.6.6 and the user data is stored in the /storage/accounts folder. I do not know if that changes anything.

What does your form look like?

It looks like:

<form class="edit-mail-form" method="POST">
      <div class="field span2">
        <label for="new-email"><?= t('new-email-address') ?></label>
        <input type="email" name="new-email" id="new-email" required>
      </div>
      <div class="field span2">
        <input type="submit" name="submit-mail-change" id="submit-mail-change" value="<?= t('edit-email-address') ?>">
      </div>
    </form>

Does get('submit-mail-change)` actually return anything, i.e. does the submit input actually have a value? I have a feeling that your code never goes past the if-statement. Try if it works without the second condition or make sure the submit input actually has a value.

Yes, good point, but also after removing the second condition, it does not change the behaviour.
But I tried changing the URL in the second if clause go($page->url());, but submitting the form still reloads the same page. So there has to be something wrong.

This works for me:

<?php

return function ($kirby, $page) {

    $error = false;
    $user  = $kirby->user();
    $data  = [];
    if ($kirby->request()->is('POST') && get('submit-mail-change')) {
  
        try {
            $data = $kirby->request()->data();
            $user->changeEmail($data['new-email']);
        } catch (Exception $e) {
            $error = $e->getMessage();
        }

        if ($error === false) {
            go($page->url());
        }

    }

    return compact('error', 'data');
};
<form action="" class="edit-mail-form" method="POST">
  <div class="field span2">
    <label for="new-email"><?= t('new-email-address') ?></label>
    <input type="email" name="new-email" id="new-email" required value="<?= $data['new-email'] ?? '' ?>">
  </div>
  <div class="field span2">
    <input type="submit" name="submit-mail-change" id="submit-mail-change" value="submit">
  </div>
</form>
<?php dump($kirby->user()) ?>
1 Like

Okay, as always, the error was very simple…
The name of my controller was wrong, I mixed up snippet and template names.
So sorry for wasting your time! But still thank you for helping, since your questioning of the controller even pointed me to double-checking it again.

1 Like