Why Is Devise Not Displaying Authentication Errors on Sign in Page

Why is devise not displaying authentication errors on sign in page?

A login with blank/wrong fields does not trigger (your) validations on the model, and therefore won't show your validation errors !

if you debug with byebug (in the first line of your view for example), you'll notice

resource.errors.count # => 0
flash # => ....@flashes={"alert"=>"Invalid email or password."}

Devise populates the "alert flash" with specific sign in error messages unique to this context of sign-in.

Why do you not see all model validation error messages ? Because that wouldn't make sense : suppose your model has a mandatory :gender attribute with validates_presence_of :gender. If normal model errors were added, then you would also see "gender cannot be blank" in the list of errors when your user tries to sign in with a wrong login :oops:.

devise_error_messages! is a specific devise method meant to show those specific errors. You can think of it as a partial validation on the fields that are used for sign in (and that are defined in your devise config file)

WORKAROUND :

If you really want to show all your error messages, you could just explicitely run the validations :

at the beginning of devise_error_messages!

resource.validate # It will generate errors and populate `resource.errors`

I believe it shouldn't mess up with other actions that already work well (register, etc.)

Devise not displaying error messge during an authentication failure?

Atlast after some good amount of searching/browsing I found the answer,

you have to add the following piece of code in our application.html.erb file

  <%- flash.each do |name, msg| -%>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
<%- end -%>

After adding this I was able to see the sign_in failures alert messages :).

Devise log_in errors not showing

The question was tagged with 'devise' so I assume you are using it. Devise errors are not flash messages and not implementing this mechanism. Take a look into this partial devise_error_messages!. You could include it inside your Devise form.

Update

I am updating my answer following the question updates. I've created a new app with three Devise models as described and used the code snippet from the question to generate the flashes. I've followed the tutorial from the question description and was able to reproduce your issue. It appears that the flash.clear in the concern clears the flash when user redirects back to the new session view.

You could add the following:

flash.clear unless params['action'] == 'new'

as a quick hack to prevent the flash clear on redirects back to new session page.

Rails 4 - devise not displaying authentication errors

Errors on Devise log-in page are added as flash messages.

In order to capture them, add following lines in app/views/layouts/application.html.erb

  <% flash.each do |name, msg| %>
<%= content_tag :div, msg %>
<% end %>

As per Devise Documentation

Remember that Devise uses flash messages to let users know if sign in
was successful or failed. Devise expects your application to call
flash[:notice] and flash[:alert] as appropriate. Do not print the
entire flash hash, print only specific keys. In some circumstances,
Devise adds a :timedout key to the flash hash, which is not meant for
display. Remove this key from the hash if you intend to print the
entire hash.

Devise login with username, signin failure not showing error message

I think you are not displaying the flash messages.
So add the below in your application.html.erb above <%= yield %>

<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "flash #{key}" %>
<% end %>

Why devise errors doesn't display on sign up page?

It's because you're using f.input_field which tells simpleform to strip away all divs around the form field. If you switch to f.input, simpleform will wrap your fields in the div that Rails will look to add the error messages to. This may break some of your styling since now all form fields will have an extra div wrapper but you can always then make your own custom error messages at the top of the form. Ex.

<% if @some_object.errors.any? %>
<ul>
<% @some_object.errors.each do |error,msg| %>
<li> <%=error.capitalize%> <%=msg%></li>
<% end %>
</ul>
<% end %>

Devise login doesn't work / no error messages

The answer was rather simple all along

I had to change <form class="sign-box"> to <div class="sign-box">
cause apparently you can't have a ruby form_for tag inside a html form attribute.



Related Topics



Leave a reply



Submit