Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-allauth signin with google redirects to development url even in production

I created a Client ID and Client Secret from the google cloud api console and added a record in the Social apps table for django-allauth

I also added WEB ORIGIN:

  1. mysite.com (prod)
  2. http://localhost:8000 (dev)

and REDIRECT URI

  1. http:mysite.com/accounts/google/login/callback/ (prod)
  2. localhost:8000/accounts/google/login/callback/ (dev)

in the google api console.

Sign in with Google works great in development and redirects to the localhost callback url on successful sign-in. But I'm getting a redirect_uri_mismatch error in prod.

These are the error details from the google error page:

Request Details

cookie_policy_enforce=false
scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
response_type=code
access_type=online
redirect_uri=http://127.0.0.1:8000/accounts/google/login/callback/
state=SOME_RANDOM_NUMBER
display=page
client_id=MY_CLIENT_ID

The redirect_uri is still set to 127.0.0.1 instead of http:mysite.com/accounts/google/login/callback/

So how do I set the proper redirect_uri?

This is my settings.py pertaining to django-allauth

INSTALLED_APPS = (
    #the usual stuff
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
)

import django.conf.global_settings as DEFAULT_SETTINGS

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
    "allauth.account.context_processors.account",
    "allauth.socialaccount.context_processors.socialaccount",
)

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)

ACCOUNT_EMAIL_REQUIRED = True

LOGIN_REDIRECT_URL = "/"

Here's urls.py

urlpatterns = patterns('',

  url(r'^accounts/', include('allauth.urls')),

)

I haven't made any other django-allauth settings. I saw the docs and couldn't find where to make the change.

like image 941
Pramod Avatar asked Nov 14 '13 23:11

Pramod


People also ask

How do I use allauth in Django?

Django-allauth settings.py is used to configure Django-allauth. After installing the package, add django-allauth to INSTALLED_APPS in settings.py to register it. Essentially, you can include any of the providers you need to register.

How to integrate Google oAuth with Django?

To integrate Google OAuth features into our app, we will use django-allauth. Then register django-allauth by adding it to INSTALLED_APPS in settings.py. The line allauth.socialaccount.providers.google specifies the OAuth provider since django-allauth supports many OAuth providers.

How to set up social login in Django using Django-allauth?

Create a Django project if you already don’t have one. Install django-allauth using the command pip install django-allauth Add, allauthallauth.account, allauth.socialaccount and all the social login features you need to INSTALLED_APPS section in settings.py. You can view the entire list of supported API's here.

How to get more user information from Google using Django?

To get more user information from Google, your app needs to be verified. You can integrate Google OAuth into your Django application with Django OAuth packages like django-allauth. You can also integrate other OAuth services similarly using django-allauth.


1 Answers

I found that the issue occurs because the nginx proxy, which sits in front of the python app server, sets the HTTP Host header to localhost.

So when allauth tries to do request.build_absolute_uri, HTTP_HOST is localhost.

So I set the Setting proxy_set_header in the nginx configuration file which fixed the issue.

proxy_set_header Host $http_host;

Also see Facebook oauth authentication is redirecting to localhost instead of to my domain for the same issue in a different avatar.

like image 109
Pramod Avatar answered Nov 20 '22 09:11

Pramod