ruby-on-railsdevisedevise-confirmable

Devise - how to allow change of unconfirmed email (correction) on verification page


I'm currently implementing Devise Confirmable so that a user is sent to the confirmations page "We've sent a validation email to , check it and click on the link".

I'm being asked to implement a feature that allows a user to edit that email in case they made a mistake, and for to update and send the verification to the new email. This would only be for brand-new accounts, and not existing ones with any data in them.

The user is not logged in before they verify, and I'm sending params[:uuid] to the page, which is getting wiped whenever it reloads - not a great way to do this. I could use localstorage, I suppose...

has anyone done this before?


Solution

  • I would store unconfirmed account ID in the session on the server: session[:unconfirmed_account] = 999. And then, when a user is not authenticated and there is session[:unconfirmed_account] present, show a notification on the page with the account unconfirmed email and a link or a button to change it.

    I think the best way to set :unconfirmed_account variable in the session is by overriding Devise's Devise::RegistrationsController#create method. This is where a new unconfirmed account is created.

    class RegistrationsController < Devise::RegistrationsController
      def create
        super do |resource|
          if resource.persisted?
            session[:unconfirmed_account] = resource.id
          end
        end
      end
    end
    

    The information about how to customize Devise controllers can be found here https://www.rubydoc.info/github/plataformatec/devise#Configuring_controllers

    Once the user has confirmed the email and is authenticated, the session variable should be deleted.

    class ConfirmationsController < Devise::ConfirmationsController
      def show
        super do |resource|
          if resource.errors.empty?
            session.delete(:unconfirmed_account)
          end
        end
      end
    end
    

    When the user updates the unconfirmed email, the account should be updated and a new confirmation message should be sent to the new email address. This is for you to implement )