I am working on a django application, where I have implemented login with Google Authentication. I have my application on a VM and use an IIS reverse proxy to redirect localhost to the live URL. The problem is that my response from Google starts localhost:8000, instead of my domain.
So how can I make sure that the response starts with my domain?

Aman GuptaPosted Jul 24, 2024, 5:56 PM
Hi Rohit,
Here is the approach, to ensure that your Google Authentication response starts with your domain instead of
localhost:8000, you need to configure the callback URL correctly in both your Django application and Google API console.Here’s how you can do it:
1. Configure the Google API Console:
Go to the Google API Console:
Select Your Project:
Configure OAuth Consent Screen:
Create/Configure OAuth Client ID:
https://yourdomain.com/callback). Ensure this URI matches the one you are using in your Django application.2. Update Django Application Settings:
Django Social Auth Configuration:
social-auth-app-django, ensure the following settings are updated.Set the Callback URL:
Middleware Configuration:
SECURE_PROXY_SSL_HEADERsetting insettings.py:IIS Configuration:
X-Forwarded-Protoheader tohttpsif your site uses SSL.3. Update Your VM and IIS Configuration:
IIS Reverse Proxy Setup:
URL Rewrite Module:
Here’s an example of how to configure URL Rewrite in IIS:
Open IIS Manager.
Select your site.
Open URL Rewrite.
Add Rule(s).
Select “Blank Rule”.
Configure the rule to match your needs.
/auth/complete/google/, you can rewrite it like this:By ensuring the correct configuration of your Google API Console, Django settings, and IIS reverse proxy, you can make sure that your Google Authentication response starts with your domain instead of
localhost:8000.Rohit GuptaPosted Jul 24, 2024, 9:06 AM
Nikunj and Jayraj thanks for replying. I have already done all the things you suggested. But that didn't help
NikolasPosted Jul 24, 2024, 7:06 AM
Great post. Learned a lot from this. Keep writing more. Solar
Nikunj SatasiyaPosted Jul 24, 2024, 6:46 AM
Hi Rohit,
When you're using Google Authentication (or any OAuth provider) with a Django application that is accessed through a domain name but proxied via a local server (like localhost with a specified port), you need to ensure that the OAuth callback URLs and application configuration are set correctly to reflect the domain name, not just the local address. Here's how you can address the issue where the response from Google starts with
localhost:8000instead of your domain:1. Set Correct Redirect URIs in Google API Console
First and foremost, ensure that the redirect URIs in your Google API Console are set to your domain instead of localhost. This is a common oversight that can lead to the issue you're experiencing.
https://yourdomain.com/your_redirect_uri/2. Configure Django Settings
Within your Django settings, you need to ensure that the
ALLOWED_HOSTSsetting includes your domain, and that you're constructing URLs with the appropriate domain:Ensure that
USE_X_FORWARDED_HOSTis set toTrueif your Django app relies on theHostheader forwarded by your proxy (IIS in this case), which should reflect the original host requested by the client.3. Configure IIS Reverse Proxy
Make sure that your IIS reverse proxy is correctly configured to handle the
Hostheader. This is crucial for ensuring that Django generates URLs using your domain instead of the localhost. You can do this by setting thereverseProxyconfiguration in yourweb.configfile:This configuration not only redirects requests to your Django application running on localhost but also ensures that the original host (domain name) is passed along in the
X-Forwarded-HostHTTP header.After making these changes, thoroughly test the login functionality to ensure that the redirects are occurring as expected and that the URLs generated by Django now reflect your domain rather than localhost. Use tools like your browser's developer tools to trace redirects and confirm that the correct URLs are being used.
By setting the proper redirect URIs in the Google API Console, configuring Django to acknowledge the forwarded host, and ensuring your IIS setup forwards the correct headers, your application should authenticate using your domain name in URLs instead of localhost.
Jayraj ChhayaPosted Jul 24, 2024, 6:10 AM
The issue you are facing with Google Authentication redirecting to localhost instead of your live domain is likely due to the redirect URI configuration. To ensure that the response starts with your domain, you need to update the Authorized Redirect URIs in the Google Cloud Console to include your live domain URL.
You should add both the localhost and live domain URLs to the Authorized Redirect URIs. This way, Google will allow redirects to both URLs, ensuring that the response starts with your domain when using Google Authentication in your Django application.
Make sure to update the redirect URIs in the Google Cloud Console for your project to resolve this redirection issue.