2012-06-30 12 views
5

के बजाय बैक एचटीएमएल भेजता है मैंने जेसन प्रमाणीकरण सेट करने में कामयाब रहा है। मैंने निम्नलिखित कोड लागू किया:जेसन के माध्यम से डेविस विफलता प्रमाणीकरण जेसन

class Users:: SessionsController < Devise::SessionsController 
    def create 
    respond_to do |format| 
     format.html { super } 
     format.json { 
     warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure") 
     render :json => {:success => true} 
     } 
    end 
    end 

    def destroy 
    respond_to do |format| 
     format.html {super} 
     format.json { 
     Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name) 
     render :json => {} 
     } 
    end 
    end 

    def failure 
    render :json => {:success => false, :errors => ["Login Failed"]} 
    end 
end 

यह ठीक काम करता है, लेकिन प्रमाणीकरण विफल होने पर, विफलता जेसन विफलता वापस नहीं करती है। मेरे पास तैयार करने के लिए एक कस्टम विफलता है। अगर मैं redirect_url को हटा देता हूं या पूरी तरह से ग्राहक विफलता को हटा देता हूं, तो प्रमाणीकरण विफलता संदेश के साथ एक जेसन लौटाता है। मेरे कस्टम विफलता इस प्रकार है:

class CustomFailure < Devise::FailureApp 
    def redirect_url 
    #return super unless [:worker, :employer, :user].include?(scope) #make it specific to a scope 
    '/' 
    end 

    # You need to override respond to eliminate recall 
    def respond 
    if http_auth? 
     http_auth 
    else 
    redirect 
    end 
end 

अंत

वैसे भी

रीडायरेक्ट अगर इसकी एक एचटीएमएल अनुरोध रखने के लिए, और एक विफलता संदेश अगर इसकी एक json अनुरोध के साथ एक json वापस जाने के लिए?

धन्यवाद!

उत्तर

5

आपको उस कस्टम विफलता का उपयोग करने के लिए वार्डन को बताना होगा।

def failure 
    respond_to do |format| 
    format.html {super} 
    format.json do 
     warden.custom_failure! 
     render :json => {:success => false, :errors => ["Login Failed"]} 
    end 
    end 
end