5
votes

After registration, which needs confirmation, my app redirects to an authenticated page so authentication fails and Devise redirects to the login path.

My flash message after registration is lost because of the second redirect.

Is there somewhere I can add a flash.keep before redirecting to the login path, in application_controller.rb or in a helper? I'd prefer not to override a devise controller for this if there's an alternative.

3
Any reason you don't want to override the controller? That is the proper solution, just change after_sign_up_path_for. - Leonel Galán
Ok yes will do Thanks - rigyt

3 Answers

5
votes

After registration, I store a flash message in the session before the redirection to the login path kicks in (because the user is unconfirmed it's "after_inactive_sign_up_path_for()")

Devise Registrations controller:

class RegistrationsController < Devise::RegistrationsController
  protected
    def after_inactive_sign_up_path_for(resource)
      # store message to be displayed after redirection to login screen
      session[:registration_flash] = flash[:notice] if flash[:notice]
      super
    end
end 

Then I show this message if it's present during the login request. Devise Sessions controller:

class SessionsController < Devise::SessionsController
  def new
    flash[:notice] = session.delete(:registration_flash) if session[:registration_flash]
    super
  end
end 
0
votes

Updating @rigyt's answer to newest devise using the Devise Failure App methodology

Create a custom failure app as shown in the link above.

lib/devise_failure.rb :

  def respond
    if http_auth?
      http_auth
    else
      # From original Devise Failure App
      store_location!
      if flash[:timedout] && flash[:alert]
        flash.keep(:timedout)
        flash.keep(:alert)
      else
        flash[:alert] = i18n_message
      end

      # Store flash temporarily in session because devise strips it
      session[:login_flash] = flash.to_hash

      redirect_to new_user_session_path
    end
  end

sessions_controller :

  def new
    flash_from_session
    super
  end

  def flash_from_session
    if session[:login_flash]
      session[:login_flash].each do |arr|
        flash[arr.first] = arr.last
      end
      session.delete(:login_flash)
    end
  end

This will set the flashes as expected and remove them from the session. I've also found it works well with pivotal's cacheable_flash

-1
votes

Why not just change the default devise flash message to whatever you want your message to say? You can edit the flash message in the devise.en.yml file. I think the one you are looking for is under registrations signed_up_but_unconfirmed