Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise: When resending confirmation email, change flash message

I'm setting up Devise such that users can log in and use the site without having confirmed their email address, similar to this question. But there are a couple of features on the site that users can't use unless they've confirmed.

OK, that's fine. I can check for current_user.confirmed?. If they're not confirmed, I can put a button on the page to have them request the confirmation be sent again.

The issue I'm having is that when they do this while logged in, the flash message they see on the result page is "You are already signed in." Which isn't ideal - I just want to put up the message that the confirmation was sent.

I'm starting down the path of trying to figure out which method of the Devise::ConfirmationController to override, and to what, but I'm hoping someone has done this already.

like image 235
dpassage Avatar asked Feb 18 '23 20:02

dpassage


2 Answers

The reason the flash says "You are already signed in" is because the user is being redirected to new_session_path from the after_resending_confirmation_instructions_path_for method. I would override this method to check if they are logged in. If they are, then don't redirect to new_session_path, set your flash message and redirect to another page.

Override the confirmations controller by putting it in controllers/users/confirmations_controller.rb

class Users::ConfirmationsController < Devise::ConfirmationsController

  protected

  def after_resending_confirmation_instructions_path_for(resource_name)
    if signed_in?
      flash[:notice] = "New message here" #this is optional since devise already sets the flash message
      root_path
    else
      new_session_path(resource_name)
    end
  end
end

Add your confirmationsController to routes->

devise_for :users, :controllers => {:confirmations => 'users/confirmations' }
like image 105
flynfish Avatar answered Feb 23 '23 07:02

flynfish


I think it should look something like this:

module Devise
  module ConfirmationsController
    extend ActiveSupport::Concern

    included do
      alias_method_chain :show, :new_flash
    end

    def show_with_new_flash
      # do some stuff
      flash[:notice] = "New message goes here"
    end
  end
end
like image 38
Mikhail Kochegarov Avatar answered Feb 23 '23 06:02

Mikhail Kochegarov