解決

Share session (cookies) between subdomains in Rails?

Ultimately I found that I needed to state the tld_length (top level domain length) in that expression. The default tld_length is 1 while example.lvh.me has a tld_length of 2 and 127.0.0.1.xip.io has a tld_length of 5, for example. So what I had in the session_store.rb file for subdomains on lvh.me in development and whatever else in production was the below.

/config/initializers/session_store.rbRails.application.config.session_store にて、

  • domain: :all
  • tld_length: x

を設定する。

# tld_lengthに2を設定した場合[]が共有範囲となる
test.[dev.example.com]
tesst.developer.[dev.example.com]

事象

サービス、管理毎に作成したAPIだが、同じプロジェクト、サブドメインでアクセスを分ける手法を取っていた。 しかし、ログイン時に毎回session(cookieのsession id)をリセットしてしまい、同一ブラウザでsessionを共存できない。