You are pretty much there. Although, I have a feeling that you can mix apples with oranges ...
Sessions:
Very often, in dynamic websites, I would like to store user data between HTTP requests (because http has no status and you cannot link the request to another request otherwise), but you do not want this data to be readable and / or editable on the client side inside the url (e.g. yourwebsite.com/yourPage?cookie=12345&id=678) etc ... because you don’t want the client to play around with this data without going through your server code.
One way to solve this problem is to save this server-side server information, give it a "session_token" (as you called it), and let the client know (and send each HTTP request) this token. This is how the session is performed.
Cookies
The most common method for implementing sessions in Rails involves the use of cookies, which are small pieces of text placed in a user's browser. Since cookies are stored from one page to another, they can store information (for example, session_token or something else that you want) that the application can use to retrieve a registered user from the database.
Where is the session stored in Rails?
Using both of the above concepts, I can now tell you that the default session store is inside the Rails CookieStore , which is about 4 KB in size.
Simply put...
def sign_in(user) @current_user = user session[:session_token] = user.reset_token! end
... that you defined puts the user in a temporary session.
Then the idea is that the following ...
def current_user @current_user ||= User.find_by_session_token(session[:session_token]) end
... the method will find and retrieve the user from the database corresponding to the session token, and initialize it with the variable you specify.
Additional Information:
It should also be noted that there is an important difference between Rails session methods and helper cookies ...
They generate cookies, however, the session[...] method creates temporary cookies that expire after the browser exits, and the cookies[...] method creates a persistent cookie that you don’t do.
In addition, I would suggest looking at section 2 of the Ruby on Rails Security Guide . You may find this helpful.
Hope this helps you.