How to share user session to different top level domains belonging to the same rails app?

Hello, I am copying my question on stackoverflow here.

I have a single rails application running on one unicorn server ,
where a user can register and create a public profile. This profile is under a subdomain that the user specifies and a top level domain based on the country of the user.
For example and and so on.

The ‘appname’ part of the url does not change and the tld length based on country is not fixed.
The base domain of the application is Through that the user can access functionality like searching for public profiles. He signs in
under the domain and devise is used for user authentication.

Problem: If he visits he will appear as non signed in. For * everything is ok.

How the user session gets shared across all domains of the application? I searched about it but it is not clear to me how to solve this. One thing I suspect is that the session must be stored in the database but what happens with the cookies? When a user visits * and he already has a session and a cookie for *, how I know who is the user to get his session?

Are there any common practices or gems that solve this problem?

Thanks in advance!