简体   繁体   English

如何检查用户是否已登录javascript

[英]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. 我有一个登录灯箱,需要在用户未登录时显示。灯箱完全在javascript,fancybox中实现,并使用rails部分为html。

I can see the cookie is set in chrome when logged in, but document.cookie seems to return empty. 我可以看到cookie在登录时设置为chrome,但document.cookie似乎返回空。 The reason probably has been explained here 原因可能在这里解释

How do I achieve this in javascript / erb. 我如何在javascript / erb中实现这一点。

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. 如果您使用rails执行此操作,请务必使用和滥用current_user ,因为它将是您最好的朋友。

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 Rails会话, Ruby on Rails教程

和Jordan Scales说的相同,但是让它变得更简单...... 如果@ current_user.nil干嘛?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM