Say I'm running a multi-tenant application that gives each organization its own portal via a subdomain.
Example -
orgA.application.com
orgB.application.com
- etc...
Each subdomain reads from a different schema/tenant in my PSQL db, but is otherwise the same application.
In my ApplicationController
I set the current_user
as -
def current_user
if session[:user_id]
@current_user ||= User.find_by_id(session[:user_id])
end
end
There are few admin/superusers such as myself that have a user account on each subdomain. If I log into orgA
with my user (id = 22
), then my session
gets set as user_id: 22
.
Now say I want to switch over to orgB
, where my user id is 44
. If I log into orgB
after having set my session in orgA
, is there any chance I could accidentally log myself in as the user who is 22
on orgB
?
More fundamentally, I'm trying to understand how a browser cookie session is set. From my understanding, it's a hash of variables that are encrypted and cached in the client's browser. Is that set per subdomain? Or do all subdomains of a particular site share the same cache/session cookie?
More importantly, how do I prevent cross pollination of sessions like in the example above? Is my current_user
method too basic?
Thanks!