6

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.

Community
  • 1
  • 1
ShaggyInjun
  • 2,880
  • 2
  • 31
  • 52

2 Answers2

3

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jordan Scales
  • 2,687
  • 4
  • 28
  • 37
  • Thanks, I want to show the lightbox on form submission. This is already working. When the user logs in, I need to submit the original form without any more user interactions. The ajax login call does add a cookie. By using the method you mentioned, I would have to refesh the page to know if the user has logged in to be able to submit the original form. – ShaggyInjun Jun 03 '12 at 09:03
  • When the page is refreshed the form data is lost unless I take the complex route of saving the form data to browser memory and fill the form back in when the page loads. If I am able to check the cookie, it will be easier. Rest is enabled in the project. Is there a devise session controller method that I can make an ajax call to verify user is logged in? – ShaggyInjun Jun 03 '12 at 09:07
  • @ShaggyInjun okay I think I know what you're asking. Do you check if the login credentials are correct before hiding the lightbox? If so, when your program responds (with AJAX) "yes, these credentials are correct," you can also send the necessary userdata you want, then handle the rest with javascript. Maybe something like `$('#welcome').text(data.username);` and if your lightbox is a form `:remote => true`, then you can handle this all with `create.js.erb` under `app/views/sessions`. It depends how your application is structured. – Jordan Scales Jun 03 '12 at 12:33
  • So, I think what you are saying is pass the original form data back and forth between rest calls, so its available ? -> Show lightbox -> get the original form data and pass along with signon request -> return the original form data with the response -> refresh page -> populate data back in the original form. Trouble is if I do that, once I refresh the page it will lose all state including any data I store in javascript variable. – ShaggyInjun Jun 03 '12 at 18:17
  • @ShaggyInjun well once the page is refreshed, the user will already be logged in - so I don't see a problem them. Are you asking how to let the user log in with AJAX? I see the flow as this. Lightbox pops up -> User enters data -> AJAX call to server -> Response saying "all good, Jordan has logged in" -> JS takes that response and places the username somewhere on the page. All of that without reloading. – Jordan Scales Jun 03 '12 at 20:53
  • 1
    Thanks for the replies. I lost track and forgot that the return object actually returns a sessionId. As long as I have this I am gold. – ShaggyInjun Jun 03 '12 at 22:16
2

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

Amol Pujari
  • 2,280
  • 20
  • 42