Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-allauth Not Saving Social Info

So I'll give full disclosure from the get-go that I am quite new to both Django and django-allauth.

Now that that is out of the way, the problem that I am having is that when a user logs in via a social site, (I have been trying Google and Facebook), none of the data retrieved from the site is pulled into the user's data fields. After authenticating, the user is still prompted to enter an email, and all name fields are left blank. I tried to fix this manually by creating a custom adapter, but that did not work either. From using print statements, I can see that the data is being fetched from the site just fine -- it just isn't being saved to the user's attributes.

Correct me if I'm wrong, but by reading the documentation and the some of the source of django-allauth, I am under the impression that social authorization automatically saves the user's email and first and last names via the populate_user(self, request, sociallogin, data): hook in the DefaultSocialAccountAdapter class, so I really shouldn't even have to deal with workarounds. Thus, I'm guessing that I am just doing something foolish that is messing this up for me... Although if there is a clever workaround that will fix this problem, I'd be fine with that, for lack for a better solution.

Note: Using Django 1.7 and Python 3.4.1

EDIT: Django-allauth is succeeding in creating a User and linking the user to a social account, which contains all of the data fetched from the social site, but none of that data is populating the fields within the User object, like email, first_name, and last_name.

Here are my django-allauth configuration settings in settings.py:

ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "required"
ACCOUNT_USERNAME_REQUIRED = False
SOCIALACCOUNT_AUTO_SIGNUP = True

# The following line was uncommented when I was trying to use my own adapter
# SOCIALACCOUNT_ADAPTER = 'profiles.profile_adapter.ProfileAdapter'

SOCIALACCOUNT_PROVIDERS = {
    'facebook':
        {   'SCOPE': ['email'],
            'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
            'METHOD': 'oauth2',
            'LOCALE_FUNC': lambda request: 'en_US'},
    'google':
        { 'SCOPE': ['https://www.googleapis.com/auth/userinfo.profile'],
          'AUTH_PARAMS': { 'access_type': 'online' } },
}

And here is the code I had in my custom adapter (Which, by using print statements, I could tell was getting used and processing the correct data) where I tried to manually save the fields into the user object

from allauth.socialaccount.adapter import DefaultSocialAccountAdapter

class ProfileAdapter(DefaultSocialAccountAdapter):

def pre_social_login(self, request, sociallogin):
    '''
    Check for extra user data and save the desired fields.
    '''
    data = sociallogin.account.extra_data
    user = sociallogin.account.user
    print("LOGS: Caught the signal -> Printing extra data of the account: \n" + str(data))
    if 'first_name' in data:
        user.first_name = data['first_name']
    elif 'given_name' in data:
        user.first_name = data['given_name']
    if 'last_name' in data:
        user.last_name = data['last_name']
    elif 'family_name' in data:
        user.last_name = data['family_name']
    user.save()

Note The above code creates a user in the database that is not linked to any social account, but contains the correct first and last names. Then the user is redirected to a form saying they are logging in with a social account and is prompted for an email address. Once this form is submitted, the original user created is overwritten by a new user that is linked to a social account, contains the email entered into the form, but does not have first or last name fields populated.

like image 427
rfj001 Avatar asked Jan 21 '26 14:01

rfj001


1 Answers

The problem was that when an email was not included with the data fetched from the social media site, django-allauth would ask for an email in a subsequent form to create the account with. When the account is then created from this form, django-allauth would not use the data fetched from the social media to populate fields. I think that this is a problem with django-allauth.

like image 65
rfj001 Avatar answered Jan 24 '26 13:01

rfj001