2014-10-07 20 views
6

Sto provando a scavalcare il metodo destroy da SessionsController di Devise, ma non ho ancora avuto alcun successo. L'ho già fatto per il metodo create, ma non so perché non funzioni per il metodo destroy.Overriding devise SessionsController destroy

Questo è il mio SessionsController:

module Api 
    module V1 
    class SessionsController < Devise::SessionsController 
     skip_before_filter :verify_authenticity_token, if: :json_request? 

     def create 
     resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure") 
     resource.update_token 
     sign_in_and_redirect(resource_name, resource) 
     end 

     def sign_in_and_redirect(resource_or_scope, resource=nil) 
     scope = Devise::Mapping.find_scope!(resource_or_scope) 
     resource ||= resource_or_scope 
     sign_in(scope, resource) unless warden.user(scope) == resource 
     return render :json => {:success => true} 
     end 

     # DELETE /resource/sign_out 
     def destroy 
     puts "DELETE /resource/sign_out" 

     return render :json => {:success => true} 
     end 

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

     protected 

     def json_request? 
     request.format.json? 
     end 
    end 
    end 
end 

se uso il curl richiesta di seguito, il metodo di creare funziona bene:

curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:3000/users/sign_in -d '{"user":{"email":"[email protected]", "password":"TopTier2011"}}' 

Ma quando uso questo:

curl -X DELETE -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:3000/users/sign_out 

Ricevo <html><body>You are being <a href="http://localhost:3000/">redirected</a>.</body></html> come risposta e puts "DELETE /resource/sign_out" la chiamata non si verifica mai.

Questo è ciò che ottengo in Rails STDOUT uscita:

Started DELETE "https://stackoverflow.com/users/sign_out" for 127.0.0.1 at 2014-10-07 14:51:40 -0200 
Processing by Api::V1::SessionsController#destroy as JSON 
    Parameters: {"session"=>{}} 
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. 
Redirected to http://localhost:3000/ 
Filter chain halted as :verify_signed_out_user rendered or redirected 
Completed 302 Found in 278ms (ActiveRecord: 0.0ms) 

voi e scusate Grazie per il mio inglese!

risposta

20

Probabilmente è necessario skip_before_action :verify_signed_out_user. Dai un'occhiata a https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb linea 4.

+0

Grazie! Questo lo ha risolto – DemianArdus

+1

Per chiarire il commento di @pragma: OP ha visto un messaggio che diceva che l'azione 'destroy' non è stata effettivamente eseguita, perché è stato chiamato un' before_filter'. È un controllo per vedere se in effetti nessuno è loggato. Per evitare di essere reindirizzati a causa di questa condizione, devi saltare questo specifico 'before_filter' – sameers