简体   繁体   English

购物车与Devise - session_id登录后更改

[英]Shopping cart with Devise - session_id changes after signing in

I try to implement a shopping cart. 我尝试实现购物车。 I store session_id in a database, and related products which are on a shopping cart. 我将session_id存储在数据库中,以及购物车上的相关产品。 After signing in I would like to have the same session and be able to connects these products with user by accessing session_id. 登录后,我想拥有相同的会话,并能够通过访问session_id将这些产品与用户连接。 But session_id seem to be different after logging in. How can I prevent from this behaviour (I believe Warden do this), or if it's wrong, how should I implement it? 但登录后,session_id似乎有所不同。我怎样才能防止这种行为(我相信Warden会这样做),或者如果它错了,我应该如何实现呢? Here is some of my code: 这是我的一些代码:

This is in application_controller in a before filter 这是在before filter中的application_controller中

 def create_session session_id = request.session_options[:id] if session_id session = Session.find_or_create_by_session_id(session_id) if session.user_id && !user_signed_in? session.update_attribute(:user_id, nil) end end end 

And this is in before filter after authentication 这是在验证后过滤之前

 def update_user_session session_id = request.session_options[:id] user_id = user_signed_in? ? current_user.id : nil session = Session.find_by_session_id(session_id) if (session && session.user_id.nil?) Session.delete_all(user_id: user_id) session.update_attribute(:user_id, user_id) end end 

The session id needs to change after every log in. It is for security reasons. 每次登录后,会话ID都需要更改。这是出于安全原因。 If you study more about sessions, you'll realise that you should not store transient data in a session. 如果您研究有关会话的更多信息,您将意识到不应该在会话中存储瞬态数据。 If you need to store some products for a particular session, why not store it corresponding to that user id instead of corresponding to a session id. 如果您需要为特定会话存储某些产品,为什么不将其存储为与该用户ID相对应而不是对应于会话ID。 If you have a cart before a user has signed in but want it to be available to the user after the user logs in, you should store the cart first in the browser local database and then transfer the results to your server after the user logs in. I hope it answers your question. 如果您在用户登录之前拥有购物车但希望用户在登录后可以使用购物车,则应首先将购物车存储在浏览器本地数据库中,然后在用户登录后将结果传输到服务器我希望它能回答你的问题。 Feel free to ask further questions. 随意提出进一步的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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