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

Social Authentication Error when setting SOCIALACCOUNT_STORE_TOKENS #3599

Closed
miqsoft opened this issue Jan 18, 2024 · 2 comments
Closed

Social Authentication Error when setting SOCIALACCOUNT_STORE_TOKENS #3599

miqsoft opened this issue Jan 18, 2024 · 2 comments

Comments

@miqsoft
Copy link

miqsoft commented Jan 18, 2024

Hello guys,

When setting SOCIALACCOUNT_STORE_TOKENS I get error save() prohibited to prevent data loss due to unsaved related object 'app'.

I assume it occurs since the SocialApp instance is not saved to database and instead created from data from the settings. However I do not know how resolve it properly.

Versions:

Django==4.2
django-allauth==0.60.1

Stacktrace:

Traceback (most recent call last):
  File "X:\venv\lib\site-packages\allauth\socialaccount\models.py", line 302, in _lookup_by_socialaccount
    t = SocialToken.objects.get(
  File "X:\venv\lib\site-packages\django\db\models\manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "X:\venv\lib\site-packages\django\db\models\query.py", line 637, in get
    raise self.model.DoesNotExist(
allauth.socialaccount.models.SocialToken.DoesNotExist: SocialToken matching query does not exist.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "X:\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "X:\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "X:\venv\lib\site-packages\django\views\decorators\csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
  File "X:\venv\lib\site-packages\django\views\generic\base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
  File "X:\venv\lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "X:\venv\lib\site-packages\django\views\decorators\debug.py", line 92, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "X:\venv\lib\site-packages\dj_rest_auth\views.py", line 48, in dispatch
    return super().dispatch(*args, **kwargs)
  File "X:\venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "X:\venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "X:\venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "X:\venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "X:\venv\lib\site-packages\dj_rest_auth\views.py", line 125, in post
    self.serializer.is_valid(raise_exception=True)
  File "X:\venv\lib\site-packages\rest_framework\serializers.py", line 227, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "X:\venv\lib\site-packages\rest_framework\serializers.py", line 429, in run_validation
    value = self.validate(value)
  File "X:\venv\lib\site-packages\dj_rest_auth\registration\serializers.py", line 161, in validate
    ret = complete_social_login(request, login)
  File "X:\venv\lib\site-packages\allauth\socialaccount\helpers.py", line 208, in complete_social_login
    sociallogin.lookup()
  File "X:\venv\lib\site-packages\allauth\socialaccount\models.py", line 281, in lookup
    if not self._lookup_by_socialaccount():
  File "X:\venv\lib\site-packages\allauth\socialaccount\models.py", line 315, in _lookup_by_socialaccount
    self.token.save()
  File "X:\venv\lib\site-packages\django\db\models\base.py", line 778, in save
    self._prepare_related_fields_for_save(operation_name="save")
  File "X:\venv\lib\site-packages\django\db\models\base.py", line 1093, in _prepare_related_fields_for_save
    raise ValueError(
ValueError: save() prohibited to prevent data loss due to unsaved related object 'app'.

Configuration:

SOCIALACCOUNT_PROVIDERS = {
    'gitea': {
        'GITEA_URL': '...',
        'VERIFIED_EMAIL': False,
        'APP': {
            'client_id': '...',
            'secret': '...',
            'key': ''
        },
    }
}
miqsoft added a commit to miqsoft/django-allauth that referenced this issue Jan 18, 2024
set token.app to None if app is not saved in database but only in settings (social authentication)
miqsoft added a commit to miqsoft/django-allauth that referenced this issue Jan 18, 2024
set token.app to None if app is not saved in database but only in settings (social authentication)
miqsoft added a commit to miqsoft/django-allauth that referenced this issue Jan 18, 2024
fix issue pennersr#3599 by checking before a token update if app instance is saved to database. If not set app instance to None.
miqsoft added a commit to miqsoft/django-allauth that referenced this issue Jan 18, 2024
fix issue pennersr#3599 by checking before a token update if app instance is saved to database. If not set app instance to None.
@pennersr
Copy link
Owner

Fixed via 5f71a37

@gabn88
Copy link
Contributor

gabn88 commented Apr 5, 2024

This does fix the error. But I'm a little worried that we lose the information for which app the socialtoken is. Could we store it as Char (in a CharField)?

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

3 participants