Rails ActiveRecord store and new session

后端 未结 3 470
轻奢々
轻奢々 2021-01-12 05:49

I am new to Rails and experience a strange issue I don\'t understand. I use ActiveRecord as a session store and need to add session id as a property of JSON responses for al

3条回答
  •  滥情空心
    2021-01-12 05:54

    Renewing is still important and you should not disable it

    Also the new session id is generated after the execution of the controller, therefore after you have a chance to set the response to be sent to the client.

    The solution is to manually trigger the renewing of the session id

    In your ApplicationController add the method:

    protected
    
      def commit_session_now!
        return unless session.options[:renew]
    
        object = session.options.instance_variable_get('@by')
        env = session.options.instance_variable_get('@env')
        session_id = object.send(:destroy_session, env, session.id || object.generate_sid, session.options)
    
        session_data = session.to_hash.delete_if { |k,v| v.nil? }
        object.send(:set_session, env, session_id, session_data, session.options)
    
        session.options[:renew] = false
        session.options[:id] = session_id
      end
    

    Then in your controller you just call this method before getting the session id for your response

    def my_action
      ...
      commit_session_now!
      render json: {session_id: session.id}, status: :ok
    end
    

    The code in commit_session_now! comes from Rack::Session::Abstract::ID#commit_session https://github.com/rack/rack/blob/master/lib/rack/session/abstract/id.rb#L327

提交回复
热议问题