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.
It's easy to check, if a user is logged in using the WordPress PHP function is_user_logged_in() .
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.
isAuthenticated() function is provided by the middleware to see if a user is currently authenticated.
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
same approach as Jordan Scales said, but making it simpler ... DRYing if @current_user.nil?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With