Handle device 401 gracefully during login

I have a very simple sign_in page for a developer. When sending incorrect data, the log shows "401 Unauthorized" and redirects me back to the sign_in page. I could not understand how to display error messages to the user.

I looked at devise::sessions_controller#createthat which looks like this:


 # POST /resource/sign_in
  def create
    resource = warden.authenticate!(auth_options)
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    sign_in(resource_name, resource)
    respond_with resource, :location => after_sign_in_path_for(resource)
  end

  def auth_options
    { :scope => resource_name, :recall => "#{controller_path}#new" }
  end  

The flow is interrupted when warden.authenticateauthentication fails and the user is redirected to "new", which is the sign_in page.

I just need to show the user the hint invalid_credentials / flash_message. So I did this by changing when authentication fails, within which I set error messages.:recall => "#{controller_path}#handle_create_fail" (look at auth_options), handle_create_fails

I am not sure that I have missed what has already been developed.

?

+3
3

'devise' rails rails, flash[:notice] flash[:alert] .

:

, Devise -, , . , "flash [: notice]" "flash [: alert]", .

, (, , ) - :

<%= content_tag(:div, flash[:error], :id => "flash_error") if flash[:error] %>
<%= content_tag(:div, flash[:notice], :id => "flash_notice") if flash[:notice] %>
<%= content_tag(:div, flash[:alert], :id => "flash_alert") if flash[:alert] %>

/:

Rails - Devise - ?

devise -

+3

, , , "alert" (sign_in.html.erb), Devise .

<div class="alert">
  <%= alert %>
</div>
0

- , , , , Rails ( -).

, Github gist: https://gist.github.com/3792471

( :user):

  • / FailureApp, devise + warden, "scoped" flash. , devise + warden - flash.now[:alert], flash.now[:user] = [:alert]
  • Expand FormBuilder(in fact,SimpleForm::FormBuilder , ), #flash, . , / FlashHash . (.. flash[:alert] , flash[:user] , :alert), , .
  • /.

Please note that the layout in my gist example does not have conditional logic around flash messages created by the layout. I did this simply to check what happens and does not appear under various circumstances.

0
source

All Articles