简体   繁体   中英

Ruby on Rails: How does cookies work?

I am new to Ruby on Rails and I came across some tutorial online about authenticating user login. I got confused about the cookies object in Rails.

When user sucessfully login, I was told to use a cookies object to store the token:

def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.hash(remember_token))
self.current_user = user
end

From the code above, seems like everytime a user login, we will store the token in the cookies object with "remember_token" symbol. I am wondering if there are multiple users login at the same time, will the token value be overwritten?

My confusion is that seems like cookies object is a singleton to hold the token value (Correct me if I am wrong). Please help me to understand what's the mechanism of how cookie object work.

Thanks

The cookies object is not a singleton object - the controller delegates this to the current request.

Each http request will have its own cookies object, initially containing all the cookies sent by the browser. Any changes will be sent back via the set-cookie header.

You may wish to read up on http cookies in general (eg RFC6265 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM