Hi all, In curl (the command line program), I can successfully log a user in using devise: def create respond_to do |format| format.json { if user_signed_in? return render :json => {:success => true, :errors => ["Already logged in."]} end resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure") return sign_in_and_redirect(resource_name, resource) } end 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, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)} end def failure return render :json => {:success => false, :errors => ["Login failed."]} end def logged_in return render :json => {:success => false, :errors => ["Already logged in."]} end However, once they are logged in, they are never logged out. So I want to be able to log out via curl and return json (curl is my testing devise right now, but ultimately I intend this to work through the iphone). I add this to users/session controller: def destroy respond_to do |format| format.json { return sign_out_and_redirect(resource_or_scope) } end end def sign_out_and_redirect(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) if Devise.sign_out_all_scopes sign_out_all_scopes else sign_out(scope) end return render :json => {:status => :signed_out} end I get a "udefined method or variable resource_or_scope". The problem is if i define this sign_out_and_redirect method in the application controller, as another forum recommended: http://stackoverflow.com/questions/4522069/rails-3-devise-how-to-get-devise-to-respond-with-json it never gets called when I do: http://localhost:3000/users/sign_out.json. And no json data is ever returned. And through curl, a strange thing happens with this: curl -X POST http://localhost:3000/users/sign_out.json -d '''' it says no route for "users/sign_out", which is odd it says that, since users/sign_out gets invoked fine on html page. thanks for response -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2011-Nov-03 15:11 UTC
Re: getting devise to return json data when signing out
I also asked this on stackoverflow: http://stackoverflow.com/questions/7997009/rails-3-getting-devise-to-return-json-data-when-signing-out I am finding it difficult to resolve this issue. On Nov 2, 7:43 pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote:> Hi all, > > In curl (the command line program), I can successfully log a user in > using devise: > > def create > respond_to do |format| > format.json { > if user_signed_in? > return render :json => {:success => true, :errors => > ["Already logged in."]} > end > resource = warden.authenticate!(:scope => > resource_name, :recall => "#{controller_path}#failure") > return sign_in_and_redirect(resource_name, resource) > } > end > 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, :redirect => > stored_location_for(scope) || after_sign_in_path_for(resource)} > end > > def failure > return render :json => {:success => false, :errors => ["Login > failed."]} > end > > def logged_in > return render :json => {:success => false, :errors => ["Already > logged in."]} > end > > However, once they are logged in, they are never logged out. So I want > to be able to log out via curl and return json (curl is my testing > devise right now, but ultimately I intend this to work through the > iphone). > > I add this to users/session controller: > > def destroy > respond_to do |format| > format.json { > return sign_out_and_redirect(resource_or_scope) > } > end > end > > def sign_out_and_redirect(resource_or_scope) > scope = Devise::Mapping.find_scope!(resource_or_scope) > if Devise.sign_out_all_scopes > sign_out_all_scopes > else > sign_out(scope) > end > return render :json => {:status => :signed_out} > end > > I get a "udefined method or variable resource_or_scope". The problem > is if i define this sign_out_and_redirect method in the application > controller, as another forum recommended: > > http://stackoverflow.com/questions/4522069/rails-3-devise-how-to-get-... > > it never gets called when I do:http://localhost:3000/users/sign_out.json. > And no json data is ever returned. > > And through curl, a strange thing happens with this: > > curl -X POSThttp://localhost:3000/users/sign_out.json-d '''' > > it says no route for "users/sign_out", which is odd it says that, > since users/sign_out gets invoked fine on html page. > > thanks for response-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.