How do I check if user is logged in javascript

javascript, jquery, ruby-on-rails

Solution

Handle this in the backend. If you're doing this with rails, be sure to use and abuse `current_user`, because it will be your best friend.

Then in your partial, you're free to have something like this

<% if current_user %>
     <p>Welcome <%= current_user.username %>!</p>
<% else %>
     <p>Please <%= link_to 'sign in', login_path %></p>
<% end %>

Rails Sessions, Ruby on Rails Tutorial

Problem

I have a signin lightbox that needs to be shown when user is not logged in. The lightbox is completely implemented in javascript, fancybox and uses rails partials for html. I can see the cookie is set in chrome when logged in, but `document.cookie` seems to return empty. The reason probably has been explained here How do I achieve this in javascript / erb.

Original source

Related problems