DRF Social OAUTH2 merge email and social login. Error UNIQUE constraint failed: users_user.email

102 Views Asked by At

implemented Django Rest Framework social OAUTH following this guide https://abhik-b.medium.com/step-by-step-guide-to-email-social-logins-in-django-5e5436e20591

I create user successfully with email/password and also with google oauth.

The issue is the following... If I create a user with email/password ([email protected]) and then I try to login with google with the same email ([email protected]) i got following error.

django.db.utils.IntegrityError: UNIQUE constraint failed: users_user.email

See complete log here

[02/May/2023 21:03:07] "POST /api-auth/google-tokens/ HTTP/1.1" 200 648
Internal Server Error: /api-auth/convert-token
Traceback (most recent call last):
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\backends\sqlite3\base.py", line 357, in execute
    return Database.Cursor.execute(self, query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.IntegrityError: UNIQUE constraint failed: users_user.email

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\core\handlers\exception.py", line 56, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\views\decorators\csrf.py", line 55, in wrapped_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\views\generic\base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\views\decorators\csrf.py", line 55, in wrapped_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\drf_social_oauth2\views.py", line 47, in dispatch
    return super(CsrfExemptMixin, self).dispatch(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\drf_social_oauth2\views.py", line 110, in post
    url, headers, body, status = self.create_token_response(request._request)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\oauth2_provider\views\mixins.py", line 124, in create_token_response
    return core.create_token_response(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\drf_social_oauth2\oauth2_backends.py", line 30, in create_token_response
    return super(KeepRequestCore, self).create_token_response(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\oauth2_provider\oauth2_backends.py", line 156, in create_token_response
    headers, body, status = self.server.create_token_response(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\oauthlib\oauth2\rfc6749\endpoints\base.py", line 112, in wrapper
    return f(endpoint, uri, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\drf_social_oauth2\oauth2_endpoints.py", line 95, in create_token_response
    SocialTokenGrant(self.request_validator).validate_token_request(request)
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\drf_social_oauth2\oauth2_grants.py", line 100, in validate_token_request
    user = backend.do_auth(access_token=request.token)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_core\utils.py", line 253, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_core\backends\oauth.py", line 437, in do_auth
    return self.strategy.authenticate(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_django\strategy.py", line 104, in authenticate
    return authenticate(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\views\decorators\debug.py", line 42, in sensitive_variables_wrapper
    return func(*func_args, **func_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\contrib\auth\__init__.py", line 77, in authenticate
    user = backend.authenticate(request, **credentials)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_core\backends\base.py", line 83, in authenticate
    return self.pipeline(pipeline, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_core\backends\base.py", line 86, in pipeline
    out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_core\backends\base.py", line 118, in run_pipeline
    result = func(*args, **out) or {}
             ^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_core\pipeline\user.py", line 73, in create_user
    return {"is_new": True, "user": strategy.create_user(**fields)}
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_core\strategy.py", line 54, in create_user
    return self.storage.user.create_user(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_django\storage.py", line 97, in create_user
    raise exc
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\social_django\storage.py", line 85, in create_user
    user = cls.user_model()._default_manager.create_user(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\contrib\auth\models.py", line 161, in create_user
    return self._create_user(username, email, password, **extra_fields)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\contrib\auth\models.py", line 155, in _create_user
    user.save(using=self._db)
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\contrib\auth\base_user.py", line 68, in save
    super().save(*args, **kwargs)
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\models\base.py", line 812, in save
    self.save_base(
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\models\base.py", line 863, in save_base
    updated = self._save_table(
              ^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\models\base.py", line 1006, in _save_table
    results = self._do_insert(
              ^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\models\base.py", line 1047, in _do_insert
    return manager._insert(
           ^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\models\query.py", line 1791, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\models\sql\compiler.py", line 1660, in execute_sql
    cursor.execute(sql, params)
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\backends\utils.py", line 102, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Luix\Estudio\React\Try-Hard-Apps\try-hard-backend\evns\tryhard\Lib\site-packages\django\db\backends\sqlite3\base.py", line 357, in execute
    return Database.Cursor.execute(self, query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.IntegrityError: UNIQUE constraint failed: users_user.email

Any Idea to merge logins? I mean, a user can login with email password or social oauth wiht same email.

Hope you can help me...

0

There are 0 best solutions below