Search code examples
djangocontainersgunicornfirebase-hostinggoogle-cloud-run

Login not working in django when using rewrites from firebase hosting to cloud run


Current Setup: I've got a Django application behind gunicorn running on Cloud Run. Since the region it is deployed in does not support Custom Domains, I have a firebase hosting setup with the following code:

{
  "hosting": {
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [{
      "source": "/**",
      "run": {
        "serviceId": "website",
        "region": "ap-south1"
      }
    }]
  }
}

The relevant settings in settings.py:

CSRF_TRUSTED_ORIGINS = ['.<domain>.com']
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True

The problem: However, the login form on /admin does not work if I access the site using my domain name https://doman.com/admin even though it works fine if I use the Cloud Run endpoint https://endpoint-uw.a.run.app.

Faulty behaviour: When accessing it from my domain, the login page shows up, I enter my credentials and log in, it adds the relevant cookies to my browser but then it redirects me back to the login page.

Could it be that since the URL is being rewritten by firebase django is expecting a cookie from uw.a.run.app? I tried adding the setting SESSION_COOKIE_DOMAIN = '.<domain>.com' but that did not fix it either, it just made the Cloud Run endpoint stop working as well.

Any advice on how to fix this or how to diagnose what is going wrong would be much appreciated, thanks!


Solution

  • The relevant settings in settings.py:

    SESSION_COOKIE_NAME = "__session"
    

    as firebase send cookie in the name "__session"