The main thing I've gathered from reading about Sessions vs Cookies is that Cookies are stored on the client-side and that sessions are stored on the server-side. If sessions are stored on the server-side, then how does the server ever know which client is theirs? Clearly, something must be on the client-side.
Whenever I use my self-rolled user authentication, I have a session_token
column in my users
database table.
Then, this module tends to facilitate sessions for me:
module SessionsHelper
def current_user
User.find_by_session_token(session[:session_token])
end
def current_user=(user)
@current_user = user
session[:session_token] = user.session_token
end
def logout_current_user!
current_user.reset_session_token!
session[:session_token] = nil
end
def require_current_user!
redirect_to new_session_url if current_user.nil?
end
def require_no_current_user!
redirect_to user_url(current_user) unless current_user.nil?
end
end
I believe by sessions being stored on the server-side, they mean the session_token
every User has. Further, the session hash must be on the client-side. If not, where is it? Notice, I'm storing the session_token of the user like this: session[:session_token] = user.session_token
. Lastly, if I'm right that the session is on the client-side, how are it and the session_token kept secure?
Lastly, is this the same way sessions tend to be handled on other frameworks (like Django, php frameworks, etc.)? If not, what are some key differences?
Many thanks.