title: Devise not working well with multiple subdomains on RoR3 application
body: <p>I have seen a lot of questions about this topic, but a lot of them have contradictory information, and for some reason it didnt work for me.</p>
  
  <p><strong>I have:</strong></p>
  
  <p>a top level domain:  i.e. lvh.me (development).
  each user has subdomains:  i.e. userdomain.lvh.me
  The login form is in the top level domain: lvh.me </p>
  
  <p><strong>I want:</strong></p>
  
  <ul>
  <li>If an user logs in, the session needs to be shared between all the subdomains. I mean, the session needs to be active in lvh.me:3000/something and userdomain.lvh.me:3000</li>
  <li>If an user logs out from lvh.me:3000/something it should work, and if the user logs out from userdomain.lvh.me:3000 it should work also.</li>
  </ul>
  
  <p><strong>I tried</strong></p>
  
  <ul>
  <li><p>Setting in an initializer the following:</p>
  
  <p>MyApplication::Application.config.session_store :cookie_store, :key => '_mykey', :domain => :all</p></li>
  </ul>
  
  <p><strong>What happened?</strong></p>
  
  <p>I can login in lvh.me:3000, I am correctly redirected to lvh.me:3000/internalpage and if I go to subdomain.lvh.me:3000 it works great. I can also logout from lvh.me:3000/internalpage <strong>BUT</strong> if I try to logout from subdomain.lvh.me:3000 it doesn't work. The destroy action in Devise SessionsController is executed and everything, but the session doesn't die.</p>
  
  <hr>
  
  <p>According to <a href="http://excid3.com/blog/sharing-a-devise-user-session-across-subdomains-with-rails-3/" rel="nofollow noreferrer">http://excid3.com/blog/sharing-a-devise-user-session-across-subdomains-with-rails-3/</a>, </p>
  
  <blockquote>
    <p>The trick here is the :domain option. What this does is sets the level
    of the TLD (top level domain) and tells Rails how long the domain is.
    The part you want to watch out for here is that if you set :domain =>
    :all like is recommend in some places, it simply won’t work unless
    you’re using localhost. :all defaults to a TLD length of 1, which
    means if you’re testing with Pow (myapp.dev) it won’t work either
    because that is a TLD of length 2.</p>
  </blockquote>
  
  <p><strong>So, after reading that I also tried</strong></p>
  
  <p>MyApplication::Application.config.session_store :cookie_store, :key => '_mykey', :domain => 'lvh.me'</p>
  
  <p><strong>What happened?</strong>
  I can login in lvh.me:3000, I am correctly redirected to lvh.me:3000/internalpage and if I go to subdomain.lvh.me:3000 it doesn't work, i have no session there. If I go back to lvh.me:3000/internalpage my session has disappeared. What happened there?</p>
  
  <hr>
  
  <p><strong>What else?</strong></p>
  
  <p>Then, after reading <a href="https://stackoverflow.com/questions/9540354/rails-3-2-subdomains-and-devise">rails 3.2 subdomains and devise</a> I changed my initializer line to </p>
  
  <pre><code>MyApplication::Application.config.session_store :cookie_store, :key =&gt; '_mykey', :domain =&gt; '.lvh.me'
  </code></pre>
  
  <p>Note the "." before the domain name.
  According to the post in SO:</p>
  
  <blockquote>
    <p>This allows this cookie to be accessible across subdomains and the
    application should maintain it's session across subdomains. May not be
    100% what you are looking for but it should get you going in the right
    direction.</p>
  </blockquote>
  
  <p><strong>What happened?</strong>
  Nothing, it didn't work. Same behavior if compared with the last thing I tried.</p>
  
  <hr>
  
  <p>I finally tried <a href="https://stackoverflow.com/questions/4060333/what-does-rails-3-session-store-domain-all-really-do">What does Rails 3 session_store domain :all really do?</a>  , creating a custom class to handle the cookies. But I had no luck.</p>
  
  <p>Of course that I deleted all the cookies and temp files before each attempt. Also I changed the name of the cookie.
  Any help? Thanks!</p>