Skip to content

Instantly share code, notes, and snippets.

@LeeHanYeong
Last active December 2, 2019 08:29
Show Gist options
  • Save LeeHanYeong/a6472f206a05f67999e58732e42d4e36 to your computer and use it in GitHub Desktop.
Save LeeHanYeong/a6472f206a05f67999e58732e42d4e36 to your computer and use it in GitHub Desktop.
Social Login username
# users/social/adapter.py
from allauth.account.utils import user_username, user_email, user_field
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.utils import valid_email_or_none
class SocialAdapter(DefaultSocialAccountAdapter):
def populate_user(self, request, sociallogin, data):
provider = sociallogin.account.provider
unique_id = sociallogin.account.extra_data['id']
username = f'{provider}_{unique_id}'
first_name = data.get('first_name')
last_name = data.get('last_name')
email = data.get('email')
name = data.get('name')
user = sociallogin.user
user_username(user, username)
user_email(user, valid_email_or_none(email) or '')
name_parts = (name or '').partition(' ')
user_field(user, 'first_name', first_name or name_parts[0])
user_field(user, 'last_name', last_name or name_parts[2])
return user
# django-allauth
ACCOUNT_LOGOUT_ON_GET = False
ACCOUNT_UNIQUE_EMAIL = False
ACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_EMAIL_REQUIRED = False
ACCOUNT_USERNAME_REQUIRED = True
SOCIALACCOUNT_ADAPTER = 'users.social.adapter.SocialAdapter'
path('facebook-login', FacebookLoginView.as_view(), name='facebook-login'),
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from rest_auth.registration.views import SocialLoginView
class FacebookLoginView(SocialLoginView):
"""
Facebook Login
---
페이스북 로그인, access_token만 전달하면 완료
"""
adapter_class = FacebookOAuth2Adapter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment