Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42828

Setup redirect URL with correct port for django-allauth

I tried to follow the tutorial at https://wsvincent.com/django-allauth-tutorial-custom-user-model/

My authorized desired callback URL is

https://localhost:2053/accounts/google/login/callback/

Under Site, I fill up as follow

enter image description here

This is how the table looks like

snapweb=# select * from django_site
snapweb-# ;
 id |  domain   |    name
----+-----------+-------------
  1 | 127.0.0.1 | example.com
(1 row)

I have the following code in settings.py

# DJANGO-ALLAUTH SETTINGS
# Site id required for using 'sites' framework with django-allauth
SITE_ID = 1    # 1 is for 127.0.0.1
ACCOUNT_DEFAULT_HTTP_PROTOCOL='https'

Then, I try to login to Google by clicking on the following link

<p><a href="{% provider_login_url 'google' %}">Log In with Gmail</a></p>

However, I get the following error from Google

enter image description here

It seems that django-allauth is passing the wrong redirect URL without correct port (https://localhost/accounts/google/login/callback/).

The correct URL should be

https://localhost:2053/accounts/google/login/callback/

But django-allauth is passing

https://localhost/accounts/google/login/callback/

May I know how I can pass the correct redirect URL to Google?


What I had tried?

For Site, I had tried various value like

snapweb=# select * from django_site
snapweb-# ;
 id |  domain        |    name
----+----------------+-------------
  1 | 127.0.0.1:2053 | example.com
(1 row)

snapweb=# select * from django_site
snapweb-# ;
 id |  domain        |    name
----+----------------+-------------
  1 | localhost:2053 | example.com
(1 row)

But still, Google is complaining receiving https://localhost/accounts/google/login/callback/

Upvotes: 1

Views: 2155

Answers (2)

Fatemeh Karimi
Fatemeh Karimi

Reputation: 987

The comment on this url from Aniket A. Aryamane says how to solve the problem:

While creating Oauth 2.0 client ID, mention the "Authorized redirect URIs" as: "http://localhost:8000/accounts/google/login/callback/" instead of "http://127.0.0.1:8000/accounts/google/login/callback/"

2) Update the Django admin - "Sites" domain name to "localhost" instead of "127.0.0.1"

Then it will work as expected.

Upvotes: 1

Arghya Saha
Arghya Saha

Reputation: 5733

As far as I understood the problem is facebook, google or any other social media login won't support localhost. You need to explicitly define a sub-domain or a particular dedicated IP address. Can you please try using a particular domain or dedicated IP?

Or else you can use this website for development https://tolocalhost.com/ and then configure it as per the you development environment. i.e the port and hostname.

Upvotes: 0

Related Questions