Use javascript to check if logged in with devise

cookies, devise, javascript, ruby-on-rails-3

Solution

Since Devise is based on Warden, you can use Warden's callbacks to set an additional cookie that will be visible from js (unlike the server only session cookie), 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

Problem

I have a rails 3 app using devise for authentication, and want to use javascript to see if a user is logged in. What's the best way to do this?

Original source