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.

3 Answers 3

25

Since Devise is based on Warden, another solution is to use Warden's callbacks, e.g in your devise.rb:

  Warden::Manager.after_set_user do |user,auth,opts|
    auth.cookies[:signed_in] = 1
  end

  Warden::Manager.before_logout do |user,auth,opts|
    auth.cookies.delete :signed_in
  end
10
  • Nice way to set a cookie for signed_in control. I tried with SessionsController as stated in accepted answer, it worked too, but it was not setting up the cookie right after the user resets its password with 'forgot password' method. It worked as expected using Warden callbacks! Commented Mar 17, 2013 at 21:10
  • 5
    @karlrosaen ~ auth.cookies is no longer supported
    – Roland
    Commented Jun 9, 2014 at 19:54
  • 1
    What should be used now that auth.cookies is not available?
    – Tom
    Commented Nov 18, 2015 at 23:13
  • 1
    @xiaopang Sorry, but I haven't used it either in a while, so I have no clue.
    – Roland
    Commented Jun 22, 2016 at 15:48
  • 1
    This method still works. I don't know if they added it back, but @roland is incorrect here. auth.cookies is still available. Commented Jun 13, 2018 at 22:55
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.

3
  • You can use the solution in stackoverflow.com/questions/4301249/… to use the original views. Commented Mar 6, 2011 at 17:20
  • Cookies are not set in iframe. After logged in, it redirects me on the login page in Ecwid embedded application.
    – Parthiv
    Commented Feb 15, 2018 at 8:41
  • 1
    you set the cookie before super - cant it be the user is not authenticated eventually (e.g. bad password)? thanks a lot
    – dowi
    Commented Feb 20, 2018 at 13:11
4

Adapted from @karl-rosaen answer this solution create a new initializer or add to the end of your devise.rb initializer.

This will add the cookie to remember the email if remember me options is set, if not it will delete the cookie

Warden::Manager.after_authentication do |user, auth, opts|
  if user.remember_me
    auth.cookies[:email] = {value: user.email, expires: 2.weeks.from_now}
  else
    auth.cookies.delete :email
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.