0

I am using tiny-cookie for saving language of 2 different projects like, abc.dev.example.com and xyz.dev.example.com as below

setCookie('language', lang)

As projects are running on localhost:3000 and localhost:8000 in dev environment locally; in the chrome debugger, for cookies domain, as shown, the domain is same i.e. localhost see screenshot below (same for localhost:3000)

chrome debugger application for localhost:8000

Now, When i Switch to localhost:3000, I am getting the language as to be tr which is intended. But, when I deploy the projects on their domains; the language is saving and showing in chrome application area but domains are different.

How to solve this issue so that it start working. I have also tried to edit the Domain for both urls (I make it .dev.meeraspace.com in chrome application) to be the same and it then start working. I have also tried below code in both projects like

setCookie('language', lang, { domain: '.dev.example.com' })
setCookie('language', lang, { domain: '.dev.example.com' }) 

Can anyone face such issue? Please, help. Thanks

Owais
  • 854
  • 2
  • 13
  • 32
  • This is not an issue - this is by design. To share the same cookie both apps have to work in the same domain - https://stackoverflow.com/questions/1612177/are-http-cookies-port-specific or you need some backend service that will share user settings between domains – Krzysztof Safjanowski Aug 24 '20 at 09:24

1 Answers1

0

I have accomplish this by making both the domain same by below code:

const url = new URL(window.location.href)
    if (url.hostname !== 'localhost') {
      const domainParts = url.hostname.split('.')
      domainParts.shift()
      const options = {
        domain: `.${domainParts.join('.')}`
      }
      setCookie('language', lang, options)
    } else {
      setCookie('language', lang)
    }

It is now getting and saving same domain i.e. .dev.example.com

I hope it can help someone.

Owais
  • 854
  • 2
  • 13
  • 32