Rails 4 persistence Create session id before and after login

I am trying to implement an ecommerce application. In it, I allow the user to view products and place them in the cart even before they login. I only prompt the user to login after verification.

However, I am losing track of the user because when the user logs in, the user's session ID changes. Because of this, I'm unable to link the items that the user places in the trash (stored in redis) to the user who posted after the user logs into the app.

Does anyone know how to get around this?

Thank.

Hooray!

+3


source to share


1 answer


Found a solution. All you need to do is set session.options [: renew] = false and the session ID will remain the same before and after you sign in.

Please refer to below implementation



class SessionsController < Devise::SessionsController
  respond_to :json

  def create
    super
    session.options[:renew] = false
  end

  def destroy
    logger.info "Logging out: #{current_user.email}; Session Id: #{session.id}"
    $redis.del "cart_#{session.id}"
    super
  end

end

      

+3


source







All Articles