0

I have set up Facebook provide via django-allauth. I want to change default extracted data. By default it's ['email', 'username', 'first_name', 'last_name', 'name']. I also need user's birthday and his profile photo.

I have tried to override DefaultSocialAccountAdapter, but I didn't find there useful methods.

Also I tried to set:

SOCIALACCOUNT_PROVIDERS = {
    'facebook': {
        'FIELDS': [
            'id',
            'email',
            'name',
            'birthday',
            'locale',
            'timezone',
            'gender',
        ],
    }
}

But nothing happend.

class SocialAccountAdapter(DefaultSocialAccountAdapter):
    def populate_user(self,
                      request,
                      sociallogin,
                      data):
        print(data)
        super().populate_user(request, sociallogin, data)

data still returns only 5 default fields

UPDATED WITH SOLUTION

from allauth.socialaccount.signals import pre_social_login
from django.dispatch import receiver
from datetime import datetime


@receiver(pre_social_login)
def populate_profile(request, sociallogin, **kwargs):
    if sociallogin.account.provider == "facebook":
        extra_data = sociallogin.account.extra_data
        sociallogin.user.birth_date = datetime.strptime(extra_data['birthday'], '%m/%d/%Y').date()
        sociallogin.user.full_name = extra_data['name']
Ernst
  • 514
  • 10
  • 26
  • 1
    take a look at my answer here: https://stackoverflow.com/questions/14523224/how-to-populate-user-profile-with-django-allauth-provider-information/48182605#48182605 – Ahtisham Jan 29 '19 at 12:42
  • @Ahtisham Is it based on post signal? – Ernst Jan 29 '19 at 12:45
  • I used `pre_social_login` signal which gets called after user is authenticated with the provider https://django-allauth.readthedocs.io/en/latest/signals.html – Ahtisham Jan 29 '19 at 12:52
  • @Ahtisham As I can see you have used `user_signed_up` signal which is used for basic login, not for sociallogin. I will not get data on the first login with `pre_social_login` signal because social account with its extra data doesn't exist at that moment – Ernst Jan 29 '19 at 14:44
  • how are you getting the data ? which signal are you using ? – Ahtisham Jan 29 '19 at 14:48
  • @Ahtisham My bad. I tried to acces via `sociallogin.user` not via `sociallogin.account` – Ernst Jan 29 '19 at 14:50
  • Did my answer on the link which I send you solved your problem ? – Ahtisham Jan 29 '19 at 14:51
  • update the question with your view. – Ahtisham Jan 29 '19 at 14:52
  • @Ahtisham Yes, your answer on another question helped me – Ernst Jan 29 '19 at 14:56

0 Answers0