The user controller of development sessions does not redirect to an error (: revocation => does not occur)

I redefine the device session controller . It works (I can log in using json), but I cannot get it to redirect to another action on error:

class Users::SessionsController < Devise::SessionsController def create resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#no") return invalid_login_attempt unless resource sign_in(resource_name, resource) #respond_with resource, :location => redirect_location(resource_name, resource) respond_to do |format| format.json { render :status => 200, :json => { :success => true, :auth_token => resource.authentication_token, :user => resource } } end end def no puts "NO!" return render:json => {:success => false, :errors => alert} end end 

I also tried:

  if resource.nil? puts "NIL!" end 

I always get the same default Devise json error in response to the wrong sign_in credentials:

 {error: "You need to sign in or sign up before continuing."} 

What am I doing wrong?

+4
source share
2 answers

Keep in mind that if the caretaker does not receive authentication from the user! action it throws a symbol to application failure. Therefore, everything that follows after this line is not related to the case of failure.

Change authenticate! on authenticate , and it will return a user object that will be processed as you see fit.

Check the proxy.rb file in the gem for more information. Documentation

+2
source

According to this similar thread ( SessionSontroller extension for JSON authentication ), you only need to include the following code:

 # config/initializers/devise.rb config.navigational_formats = [:"*/*", "*/*", :html, :json] 
+1
source

Source: https://habr.com/ru/post/1434395/


All Articles