Alex Rivera | Logout

How to set cookies when log in/out with Devise

Asked 2011-02-01T18:53:50.167
18

I am using Devise for rails 3 application. For page caching, I need to set cookies for log in/out info.

What's the simplest way to set cookies when log in/out occurrs with Devise? I read 'how to customize controller' part but it seems to be a lot of work.

Edit
Report

1 Answer

14

It actually wouldn't be too hard to extend the devise SessionsController to add cookies on log in and log out, you could create a controller similar to this:

# app/controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController
  # POST /resource/sign_in
  def create
    cookies[:sign_in] = "Sign in info you want to store"
    super
  end

  # GET /resource/sign_out
  def destroy
    cookies[:sign_out] = "Sign out info you want to store"
    super
  end
end

Then you would have to add the following to your routes.rb:

devise_for :users, :controllers => { :sessions => "sessions" }

That should get you most of the way there.

answered 2011-02-05T09:12:44.277

Your Answer