Note that there are some explanatory texts on larger screens.

plurals
  1. PORails ActiveRecord store and new session
    primarykey
    data
    text
    <p>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 all the requests. I use Devise as well if it have some impact on the situation. The problem is that if a request is made by a user without cookies (or at least without session id in the cookie) the session.id is empty or - attention, please - not the same value that is set in the response cookie.</p> <p>For debugging, I add this code as an after_filter to ApplicationController:</p> <pre><code>puts session.id puts request.session_options[:id] </code></pre> <p>Both values are the same. They match the value in the cookie if it is present. Otherwise, if session id is not present in the cookie, the cookie set after that request has different value.</p> <p>My opinion is that session_id gets new value after it is actually saved to the database, where it have to be unique. DB migration:</p> <pre><code>def change create_table :sessions do |t| t.string :session_id, :null =&gt; false t.text :data t.timestamps end add_index :sessions, :session_id, :unique =&gt; true add_index :sessions, :updated_at end </code></pre> <p>My question: How can I get the actual session.id value of a new session before the first response is rendered?</p> <p><strong>UPD:</strong></p> <p>I just created a new Rails app that uses ActiveRecord session store without Devise, and I can get session.id that is going to be set in cookie just before response with this code id application controller:</p> <pre><code>class ApplicationController &lt; ActionController::Base after_filter :show_session def show_session puts session.id end end </code></pre> <p>But in my existing app with Devise I get a value that really looks like a session id, but that doesn't match the value set in the cookie via Set-Cookie response header and the value actually saved to sessions table in database. Looks like Devise have a conflict with ActiveRecord session store in some way. Need to go deeper to figure it out.</p> <p><strong>UPD 2</strong></p> <p>Looks like I found the problem roots. As I said, I use Devise for authorization with Omniauth. According to the documentation, sign_in method resets session id for security reasons. But after that reset session.id returns the old value, that had been automatically set. I use this code as an Omniauth callback:</p> <pre><code>def facebook_access_token sign_in @user puts session.id end </code></pre> <p>And in console I get session id different from the one set in the Set-Cookie response header. If I comment "sign_in" line, these values match. New question: how can I get the new session id value after it is been reset inside of sign_in method? Is it an internal Warden/Devise implementation or something?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload