Search code examples
djangoherokucookiescross-domainsession-cookies

Cookies when using separate Dyno for react frontend and Django backend


I am building a simple web app using React.js for the frontend and Django for the server side.

Thus frontend.herokuapp.com and backend.herokuapp.com.

When I attempt to make calls to my API through the react app the cookie that was received from the API is not sent with the requests.

I had expected that I would be able to support this configuration without having to do anything special since all server-side requests would (I thought) be made by the JS client app directly to the backend process with their authentication cookies attached.

In an attempt to find a solution that I thought would work I attempted to set

SESSION_COOKIE_DOMAIN = "herokuapp.com"

Which while less than ideal (as herokuapp.com is a vast domain) in Production would seem to be quite safe as they would then be on api.myapp.com and www.myapp.com.

However, with this value set in settings.py I get an AuthStateMissing when hitting my /oauth/complete/linkedin-oauth2/ endpoint.

Searching google for AuthStateMissing SESSION_COOKIE_DOMAIN yields one solitary result which implies that the issue was reported as a bug in Django social auth and has since been closed without further commentary.

Any light anyone could throw would be very much appreciated.


Solution

  • I ran into the exact same problem while using herokuapp.com. I even posted a question on SO here.

    According to Heroku documentation:

    In other words, in browsers that support the functionality, applications in the herokuapp.com domain are prevented from setting cookies for *.herokuapp.com

    Heroku blocks cookies from frontend.herokuapp.com and backend.herokuapp.com You need to add a custom domain to frontend.herokuapp.com and backend.herokuapp.com

    The entire answer https://stackoverflow.com/a/54513216/1501643