Devise settings basket - session_id after login

I am trying to implement a cart. I store session_id in the database and related products that are in the shopping cart. After logging in, I would like to have the same session and be able to connect these products with the user by accessing session_id. But session_id seems to be different after login. How can I prevent this behavior (I believe that Warden does it), or if it is wrong, how can I implement it? Here is my code:

This is in application_controller before filter

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 happens before the 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
+4
source share
1

. . , , . , , , . , , , , , , . .

0

Source: https://habr.com/ru/post/1535806/


All Articles