Configurazione dell'app Rails per gestire più sottodomini e più cookie
-
14-11-2019 - |
Domanda
Ho un'app Rails che supporta più domini e ogni dominio può avere più sottodomini.
Utenti che visitano MyDomain1.com Non ricevere la stessa esperienza di MyDomain2.com (Sebbene il comportamento di base delle app sia lo stesso)
Pertanto, se un utente è loggato su MyDomain1.com, non dovrebbe essere connesso a MyDomain2.com
Se un utente è loggato su France.myDomain1.com, è dovrebbe quindi essere loggato su Germania.MyDomain1.com
In precedenza, l'ho gestito impostando il dominio nelle configurazioni del negozio di sessione:
.
MyApp::Application.config.session_store :cookie_store, :key => '_MyApp_session', :domain => APP_CONFIG[:domain]
Sto cercando di elaborare il modo migliore per gestirlo con più domini?
Ho provato l'hacking intorno a ActionDispatch::Callback
ma la richiesta non è disponibile da lì dentro.
Qualcuno può suggerire un buon modo per supportare più cookie da un'unica app?
Idealmente vorrei creare un cookie fresco per ogni sottodominio .
Soluzione
You should do that:
class ActionDispatch::Session::MultiDomainStore < ActionDispatch::Session::CookieStore
def initialize(app, options = {})
super(app, options.merge!(:domain => compute_domain(app)))
end
def compute_domain(app)
...
end
end
MyApp::Application.config.session_store :multi_domain_store, :key => '_MyApp_session'
I.e. your domain should start with the dot.
Altri suggerimenti
It shouldn't be an issue as cookies are only valid per domain. You can have a _MyApp_session
for example1.com
and one for example2.com
. The cookies are managed by the browser and only sent to the host if the domain matches.
Say you visit example1.com and log in and you will get a cookie with the value abcdef123
. Then you log into example2.com and you will get another cookie with a random string uvwxyz890
.
If you return to example1.com later, the browser will only send the cookies that are valid for this domain to your app. Your app won't have to manage anything and you don't have to hack anything.