1

I'm implementing social login using Facebook and LINE, but I'm facing a couple of issues:

Facebook Login Issue: After clicking the Facebook login button and entering the password to log in, the flow should ideally end there. However, I am redirected to an email input page, and even after entering the email, I get an error saying "Failed to create a unique username." LINE Login Issue: The LINE login works, but when I check the admin panel, the email field under the account section is empty. However, the email field does exist in the social login account. Could someone help me understand why these issues are occurring and how to resolve them? Any guidance would be greatly appreciated!

# users/adapters.py
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.account.utils import user_email
from django.utils.crypto import get_random_string
from .models import User

class CustomSocialAccountAdapter(DefaultSocialAccountAdapter):
    def populate_user(self, request, sociallogin, data):
        user = super().populate_user(request, sociallogin, data)
        
        email = user_email(user)
        if email:
            user.username = email
        else:
            provider = sociallogin.account.provider
            uid = sociallogin.account.uid
            user.username = f"{provider}_{uid}"
        
        while User.objects.filter(username=user.username).exists():
            user.username = f"{user.username[:25]}_{get_random_string(5)}"
        
        return user
# settings/base.py
ACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username'
ACCOUNT_ADAPTER = 'allauth.account.adapter.DefaultAccountAdapter'
SOCIALACCOUNT_ADAPTER = 'users.adapters.CustomSocialAccountAdapter'

import uuid
SOCIALACCOUNT_PROVIDERS = {
    'facebook': {
        'METHOD': 'oauth2',
        'SCOPE': ['email', 'public_profile'],
        'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
        'INIT_PARAMS': {'cookie': True},
        'FIELDS': ['id', 'email', 'name'],
        'EXCHANGE_TOKEN': True,
        'LOCALE_FUNC': lambda request: 'ja_JP',
        'VERIFIED_EMAIL': False,
        'VERSION': 'v13.0',
    },
    'line': {
        'SCOPE': ['profile', 'openid', 'email'],
        'AUTH_PARAMS': {'nonce': lambda: uuid.uuid4().hex},
        'LOCALE_FUNC': lambda request: 'ja_JP',
        'VERIFIED_EMAIL': False,
        'VERSION': 'v2.1',
    },
}

LOGIN_URL = '/users/login/'
ACCOUNT_LOGIN_URL = '/users/login/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
# error message
Internal Server Error: /accounts/3rdparty/signup/
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/socialaccount/views.py", line 40, in dispatch
    return super(SignupView, self).dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/account/mixins.py", line 40, in dispatch
    response = super().dispatch(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/account/mixins.py", line 105, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/account/mixins.py", line 69, in post
    response = self.form_valid(form)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/socialaccount/views.py", line 53, in form_valid
    return flows.signup.signup_by_form(self.request, self.sociallogin, form)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/socialaccount/internal/flows/signup.py", line 35, in signup_by_form
    user, resp = form.try_save(request)
                 ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/account/forms.py", line 388, in try_save
    user = self.save(request)
           ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/socialaccount/forms.py", line 27, in save
    user = adapter.save_user(request, self.sociallogin, form=self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/socialaccount/adapter.py", line 108, in save_user
    get_account_adapter().save_user(request, u, form)
  File "/usr/local/lib/python3.11/site-packages/allauth/account/adapter.py", line 324, in save_user
    self.populate_username(request, user)
  File "/usr/local/lib/python3.11/site-packages/allauth/account/adapter.py", line 292, in populate_username
    or self.generate_unique_username(
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/account/adapter.py", line 298, in generate_unique_username
    return generate_unique_username(txts, regex)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/allauth/utils.py", line 127, in generate_unique_username
    raise NotImplementedError("Unable to find a unique username")
NotImplementedError: Unable to find a unique username
"POST /accounts/3rdparty/signup/ HTTP/1.1" 500 133282

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.