Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnecting social account throws 500 error if not last account - adapter not initialised #3799

Closed
enzedonline opened this issue May 13, 2024 · 4 comments

Comments

@enzedonline
Copy link

enzedonline commented May 13, 2024

EDIT: I see the code in the repository has been updated, it's just not updated in the package on pypi (0.62.1)

If I try to disconnect a social account when there are multiple accounts connected, I get a 500 error with the following traceback:

[13/May/2024 00:29:48] ERROR [django.request:241] Internal Server Error: /en/accounts/3rdparty/
Traceback (most recent call last):
  File ".venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File ".venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File ".venv/lib/python3.10/site-packages/sentry_sdk/integrations/django/views.py", line 84, in sentry_wrapped_callback
    return callback(request, *args, **kwargs)
  File ".venv/lib/python3.10/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
  File ".venv/lib/python3.10/site-packages/django/utils/decorators.py", line 48, in _wrapper
    return bound_method(*args, **kwargs)
  File ".venv/lib/python3.10/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapper_view
    return view_func(request, *args, **kwargs)
  File ".venv/lib/python3.10/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
  File ".venv/lib/python3.10/site-packages/allauth/account/mixins.py", line 68, in post
    if form.is_valid():
  File ".venv/lib/python3.10/site-packages/django/forms/forms.py", line 197, in is_valid
    return self.is_bound and not self.errors
  File ".venv/lib/python3.10/site-packages/django/forms/forms.py", line 192, in errors
    self.full_clean()
  File ".venv/lib/python3.10/site-packages/django/forms/forms.py", line 328, in full_clean
    self._clean_form()
  File ".venv/lib/python3.10/site-packages/django/forms/forms.py", line 349, in _clean_form
    cleaned_data = self.clean()
  File ".venv/lib/python3.10/site-packages/allauth/socialaccount/forms.py", line 57, in clean
    flows.connect.validate_disconnect(self.request, account)
  File ".venv/lib/python3.10/site-packages/allauth/socialaccount/internal/flows/connect.py", line 40, in validate_disconnect
    adapter.validate_disconnect(account, accounts)
UnboundLocalError: local variable 'adapter' referenced before assignment

The validate_disconnect method in allauth/socialaccount/internal/flows/connect.py doesn't initialise the adapter variable if the is_last condition isn't met so the call to adapter.validate_disconnect(account, accounts) fails:

def validate_disconnect(request, account):
    """
    Validate whether or not the socialaccount account can be
    safely disconnected.
    """
    accounts = SocialAccount.objects.filter(user_id=account.user_id)
    is_last = not accounts.exclude(pk=account.pk).exists()
    if is_last:
        adapter = get_adapter()
        if allauth_settings.SOCIALACCOUNT_ONLY:
            raise adapter.validation_error("disconnect_last")
        # No usable password would render the local account unusable
        if not account.user.has_usable_password():
            raise adapter.validation_error("no_password")
        # No email address, no password reset
        if (
            account_settings.EMAIL_VERIFICATION
            == account_settings.EmailVerificationMethod.MANDATORY
        ):
            if not EmailAddress.objects.filter(
                user=account.user, verified=True
            ).exists():
                raise adapter.validation_error("no_verified_email")
    adapter.validate_disconnect(account, accounts)

Using django-allauth 0.62.1

@pennersr
Copy link
Owner

Fixed via 4b4d234

@enzedonline
Copy link
Author

Thanks.

Is there a schedule for the next release on PyPi?

@pennersr
Copy link
Owner

@enzedonline -- Yes, today :-)

@enzedonline
Copy link
Author

Brilliant - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants