Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if user is logged in javascript

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.

like image 974
ShaggyInjun Avatar asked Jun 03 '12 06:06

ShaggyInjun


People also ask

How do you check if a user is logged in or not JS?

It's easy to check, if a user is logged in using the WordPress PHP function is_user_logged_in() .

How to check if user is login or not in jquery?

Check the class attribute for body : If the theme is using body_class() the body has a class named logged-in for users that are logged in. Be aware the function can be used on the element html too. You can also just use is_user_logged_in() as a condition to enqueue or print the script.

How do I know if a user is logged in node?

isAuthenticated() function is provided by the middleware to see if a user is currently authenticated.


2 Answers

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

like image 55
Jordan Scales Avatar answered Sep 29 '22 10:09

Jordan Scales


same approach as Jordan Scales said, but making it simpler ... DRYing if @current_user.nil?

like image 40
Amol Pujari Avatar answered Sep 29 '22 08:09

Amol Pujari