Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise: Don't display "unauthenticated" flash message when redirected to users/sign_in from root path

Using Devise 2.2.4 in Rails 3.2.

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

This is desirable!

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

The redirection is still desirable, the flash message not so much.

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

like image 834
user267272 Avatar asked Feb 07 '14 18:02

user267272


Video Answer


2 Answers

You can remove the flash message in your SessionsController 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
like image 123
Markus Avatar answered Sep 20 '22 17:09

Markus


I simply went to devise.en.yml and removed the unauthenticated message, leaving it as:

unauthenticated: ""
like image 27
Abel Pc Ag Avatar answered Sep 19 '22 17:09

Abel Pc Ag