Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flash.keep when devise redirects to login path

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.

like image 385
rigyt Avatar asked Oct 05 '22 23:10

rigyt


1 Answers

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 
like image 123
rigyt Avatar answered Oct 13 '22 12:10

rigyt