Think of: do not display an "unauthenticated" flash message when redirecting to users / sign _in from the root path

Using the 2.2.4 Utility in Rails 3.2.

When you are not logged in and I go to myapp.com/documents url, Devise redirects me to myapp.com/users/sign_in and displays the flash message configured in devise.en.yml in devise.failure.unauthenticated .

This is desirable!

When you are not logged in and I go to myapp.com url (root path), Devise redirects the same way and displays the same flash message.

Redirection is still desirable, flash message is not so much.

Is there a way to configure Devise to NOT display the message "unauthenticated" if redirecting from the root path? I know I can get around this in a custom FailureApp, but this would seem to be a fairly common case where a simpler resolution should exist.

+3
source share
4 answers

You can replace before_filter :authenticate_user!or something that you use with your own code that will not set the message.

+1
source

You can delete the flash message in the SessionController after the redirect.

class SessionsController < Devise::SessionsController

  before_filter :remove_authentication_flash_message_if_root_url_requested

  private

  def remove_authentication_flash_message_if_root_url_requested
    if session[:user_return_to] == root_path and flash[:alert] == I18n.t('devise.failure.unauthenticated')
      flash[:alert] = nil
    end
  end
end
+6
source

, . , , FailureApp. , , , , Devise:

# app/config/initializers/devise.rb
class CustomFailure < Devise::FailureApp
  def redirect
    store_location!
    if flash[:timedout] && flash[:alert]
      flash.keep(:timedout)
      flash.keep(:alert)
    elsif attempted_path != root_path
      flash[:alert] = i18n_message 
    end
    redirect_to redirect_url
  end
end

Devise.setup do |config|
  config.warden do |manager|
    manager.failure_app = CustomFailure
  end
end

.

+1

, . , Devise , . THESE , Devise , .

Thanks for the input. I am going to mark Speransky's comment as an answer, since technically this is what we are already doing.

+1
source

All Articles