Skip to content

Instantly share code, notes, and snippets.

@xiaohui-zhangxh
Created December 31, 2023 10:49
Show Gist options
  • Save xiaohui-zhangxh/ac8efd93f4a9a9646977fef814360dab to your computer and use it in GitHub Desktop.
Save xiaohui-zhangxh/ac8efd93f4a9a9646977fef814360dab to your computer and use it in GitHub Desktop.
Make dymanic session store key for Rails on Multi-Tenant Arch
# Why I need this?
#
# Rails cookie session store is a static value set on boot, in config/initializers/session_store.rb
# My Rails project is multi-tenant arch, she allows tenant bind their own domains to each service(Workspace,
# CMS, Community, Wiki etc.), some domains are in the same zone, such as:
#
# - www.helo.com
# - app.hello.com
# - help.app.hello.com
#
# if three domains above are binded by our system, their cookies are messed up due to the same name 'baklib',
# this cause some weird cases we found.
#
# Thinking:
#
# If we can make each tenant service has their own cookie name, it should be fixed.
#
# How:
#
# Write a middleware.
#
# before
Rails.application.config.session_store :cookie_store, key: 'baklib', httponly: true
# after
Rails.application.config.session_store :dynamic_cookie_store, key: 'baklib', httponly: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment