Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use javascript to check if logged in with devise

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?

like image 701
Solomon Avatar asked Apr 20 '12 15:04

Solomon


1 Answers

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
like image 54
Karl Rosaen Avatar answered Nov 19 '22 13:11

Karl Rosaen