4

I followed this question Devise redirect after login fail and it works great, but I am getting a flash notice telling me that I need to login to continue, and the message I need is that the password or email is invalid. So I added the flash notice in the CustomFailure like this:

    class CustomFailure < Devise::FailureApp
  def redirect_url
    root_path
  end

  def respond
    if http_auth?
      http_auth
    else
      flash[:error] = I18n.t(:invalid, :scope => [ :devise, :failure ])
      redirect
    end
  end
end

and now it's showing me both messages, invalid password and unauthenticated, how can I eliminate the unauthenticated message?

Community
  • 1
  • 1
dcarey
  • 73
  • 1
  • 7

1 Answers1

2

Devise does not set flash[:error], but flash[:alert] when the login fails, try to set this in your application.

You could also just overwrite the method Devise::FailureApp#i18n_message and have it return the message of your choosing:

class CustomFailure < Devise::FailureApp
  def redirect_url
    root_path
  end

  def i18n_message
    "Hello World" # you might go for something more elaborate here
  end
end
yas4891
  • 4,774
  • 3
  • 34
  • 55
  • Thank you very much for your answer. That doesn't work for me, because I only want to show the message "Invalid password or email" when the login fails, and when the user is trying to enter to a page that doesn't have access if he is not authenticated I want to show the message unauthenticated. And if I overwrite the method and change the "unauthenticated" for "invalid" it will show me always the message "Invalid email or password" – dcarey Jan 16 '13 at 13:44