Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent session creation on rails 3.2.2 for RESTful api

How can i prevent the session store from creating a session on JSON/XML calls ?

My problem is that i save sessions in a database and it gets flooded on multiple api calls.

I'm using Rails 3.2.2 and Devise for authentication.

like image 405
refaelos Avatar asked May 23 '12 10:05

refaelos


3 Answers

My problem here was with Warden inside Devise. I had to "tell" Warden not to store the user in the session after the user is authenticated.

resource = warden.authenticate!(:scope => resource_name, :store => !(request.format.xml? || request.format.json?))

Hope that helps whoever sees this thread.

like image 83
refaelos Avatar answered Nov 20 '22 18:11

refaelos


resource = warden.authenticate!(:scope => resource_name, :store => is_navigational_format?)
like image 45
Altonymous Avatar answered Nov 20 '22 17:11

Altonymous


in theory if you don't use it, it is not loaded now. up until rails 2.3.8, you could do:

# application_controller.rb
session :off, :if => :sessionless_request?

protected

def sessionless_request?(request)
  request.format == :xml || request.format == :json
end 

now you can do the same with this gem https://github.com/kares/session_off

like image 1
Viktor Trón Avatar answered Nov 20 '22 17:11

Viktor Trón