5
私はjson認証を設定することができました。私は次のコードを実装しました:jsonによるデバッグの失敗認証はjsonの代わりにhtmlを返す
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
これはうまくいきますが、認証に失敗した場合、失敗はjsonの失敗を返しません。私は工夫のためにカスタムの失敗があります。 redirect_urlを削除するか、顧客のエラーを完全に取り除くと、認証はエラーメッセージとともにjsonを返します。私のカスタムの障害は次のとおりです。
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
エンド
とにかくリダイレクトその場合のhtmlリクエストを維持し、その場合の障害MSG JSONリクエストでJSONを返すには?
ありがとうございます!