4

I have a profile form where a user can update their email.

Upon doing so I would like to:

  1. Show the user a message saying they must check their email to verify the new email address.
  2. After they click the "Verify Email" address in their inbox, redirect them to a url preferably with a Flash Message letting them know they'e successfully updated their email.

Possible?

Peter Tell
  • 1,828
  • 12
  • 19

2 Answers2

5

Based on Brad's response I created the following controller in a plugin:

<?php
namespace Craft;

class Myplugin_SaveUserController extends BaseController
{
    protected $allowAnonymous = true;
    /**
     * Handle the save user form request.
     */
    public function actionSaveUser()
    {
        $this->requirePostRequest();
        $email  = craft()->request->getPost('email');
        $userId = craft()->request->getPost('userId');
        $user   = craft()->users->getUserById($userId);

        if ($email !== $user->email)
        {
            craft()->userSession->setFlash('changedEmail', 'changed email');
        }
        $this->forward('users/saveUser', false);
    }
}

I then check for it in my template:

{% if craft.session.getFlash('changedEmail') == 'changed email' %}

This seems to do the job.

For anyone who may be using this in the future, it's worth noting that I also had to change the "action" hidden input in my form:

<input type="hidden" name="action" value="myplugin/saveUser/saveUser">
Peter Tell
  • 1,828
  • 12
  • 19
4

In Craft when changing an email address (even if it's the currently logged in user), they must supply their existing password as well. You can use the hidden redirect input to send to whatever template you want (assuming saving the user was successful). In your case, then template can tell them to check their email and click the link it contains.

<form method="post" accept-charset="UTF-8">
    {{ getCsrfInput() }}

    <input type="hidden" name="action" value="users/saveUser">
    <input type="hidden" name="redirect" value="users/checkyouremail">
    <input type="hidden" name="userId" value="{{ currentUser.id }}">

    <input type="text" name="email" value="{{ currentUser.email }}">
    <input id="password" type="password" name="password">

    <input type="submit" value="Save Profile">
</form>

Once the click the link and verify they own the email address, they'll get redirected to the site root.

If you need different behavior than that, you can always write a plugin that has it's own controller actions that changes the workflow into something you need.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • Hi Brad, Thanks for the answer! I'm currently using the redirect hidden input to send them to the same profile page they've been editing from and I use craft.session.getFlash('notice') to verify what they've done. IE, a return of "User saved." means they updated their profile. Unfortunately changing emails is optional in this profile form so I would actually need a way to validate through some sort of flash message or something that they actually changed their email (so I could conditionally display Check your email instructions). – Peter Tell Jan 06 '16 at 19:58
  • In that case a custom plugin is the way to go. You'd just check if email had a value in POST and if so, set the flash message. – Brad Bell Jan 06 '16 at 20:19
  • Awesome thanks Brad. I want to give you the check mark because you led to the right answer, but technically for anyone viewing this in the future the custom plugin route and that controller were what were needed to accomplish the task. But please know that in my heart... those are your 50 internet points. – Peter Tell Jan 06 '16 at 20:34
  • Pfft... I burn my points for warmth! – Brad Bell Jan 06 '16 at 20:35
  • 1
    I thought that's what the whiskey was for ;-) Thanks again! – Peter Tell Jan 06 '16 at 20:57