Hi seeing as this is a Thoughtbot gem I thought I’d ask here first.
I’m reading through the Clearance gem’s source code (a habit I’m trying to get into to better my coding) and I’m trying to figure out what’s happening in the session management side.
In session.rb we have
def sign_in(user, &block)
@current_user = user
status = run_sign_in_stack
if status.success?
cookies[remember_token_cookie] = user && user.remember_token
else
@current_user = nil
end
if block_given?
block.call(status)
end
end
In particular
cookies[remember_token_cookie] = user && user.remember_token
So if successful the cookie is set to the user’s remember token. But then when the request is finished and the rails app returns the result back to the rack middleware stack, clearance’s rack_session sets the cookie again like so
def call(env)
session = Clearance::Session.new(env)
env[:clearance] = session
response = @app.call(env)
session.add_cookie_to_headers response[1] # <<- set again here
response
end
which is defined as
def add_cookie_to_headers(headers)
if cookie_value[:value].present?
Rack::Utils.set_cookie_header!(
headers,
remember_token_cookie,
cookie_value
)
end
end
Can anyone explain the flow?