Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly start a session using cookies, store it and access it in a Sinatra app using Rack sessions
    primarykey
    data
    text
    <p>I have a Sinatra app running on Heroku. I've had a really tough time properly setting up the cookies for my users. I've been looking at examples and documentation for a couple days now and just can't seem to crack the problem. I would like to have my users login with a email and password. If the login is successful, I want to create a cookie which expires after some set amount of time. I want the user to remain logged in if they close and re-open their browser or if I push new code to Heroku. I think that is just normal use of cookies, so if someone can help me get it going, I'd be very grateful.</p> <p>Here is how I <em>think</em> I'm supposed to setup the post '/login' route.</p> <pre><code>post '/login/?' do #do_login is a helper method which checks if the user's credentials are correct if do_login use Rack::Session::Cookie, :key =&gt; 'rack.session', :domain =&gt; 'www.Feistie.com', :path =&gt; '/', :expire_after =&gt; 2592000, :secret =&gt; 'a_30_character_key', :user_id =&gt; params[:user_id] end end </code></pre> <p>Even if that is correct, I don't really understand it. What is the :key? I'm assuming :domain should just be set to my website as I did. What about :path? Why is :path set to '/'? Is there a place I actually set the secret key for my Rack app? I'm not totally clear on how cookie security works. I'm also not sure if I can add :user_id to the hash the way I did.</p> <p>Then next part is setting up a helper "logged_in?" to see if someone is logged in?</p> <pre><code>def logged_in? !session.nil? end </code></pre> <p>That is probably wrong, but I figured I'd try. </p> <p>I also need to be able to check who the current user actually is. I used to do this by <code>session[:user_id] == @user.id</code> or something similar. The main question I have is how do I access the :user_id value in the cookie?</p> <p>Finally the logout.</p> <pre><code>post '/logout/?' do session.clear end </code></pre> <p>If you guys could get me on track with all this, it would be amazing! Thanks in advance and if you need more info, I will be glad to provide.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload