12

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.

Viktor Trón
  • 8,774
  • 4
  • 45
  • 48
refaelos
  • 7,927
  • 7
  • 36
  • 55

4 Answers4

18

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.

refaelos
  • 7,927
  • 7
  • 36
  • 55
4
resource = warden.authenticate!(:scope => resource_name, :store => is_navigational_format?)
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
Altonymous
  • 783
  • 7
  • 25
1

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

Viktor Trón
  • 8,774
  • 4
  • 45
  • 48
  • i tried your solutions and it doesn't work. maybe devise and session_off doesn't work well together ?! – refaelos May 23 '12 at 12:32
  • note that the plugin only redefines session call from controller, request.session call will still retrieve session. I think devise (actually warden) uses env['rack.session'] :( – Viktor Trón May 23 '12 at 13:24
  • i'm getting multiple errors when using session_off. i probably have several plugins that use the session. – refaelos May 23 '12 at 13:34
  • that's too bad b/c session_off seems like a great plugin. (btw, devise also uses "session") – refaelos May 23 '12 at 13:34
-4

You should use "devise :timeoutable" in your model and use config.timeout_in = 0 in config/initializers/devise.rb

Restart your server!

raphaelluchini
  • 87
  • 1
  • 1
  • 8