iMerica / dj-rest-auth

Authentication for Django Rest Framework
https://dj-rest-auth.readthedocs.io/en/latest/index.html
MIT License
1.62k stars 302 forks source link

Google registration not working with django-allauth 0.55.2 #551

Closed itsdkey closed 6 months ago

itsdkey commented 9 months ago

Hi there, After updating to django-allauth 0.55.2 (which is theoretically supported by dj-rest-auth[with_socials] ) I started getting errors when trying to register on my website via Google.

The problem is appearing in the line: 160 on dj_rest_auth/registration/serializers.py

ret = complete_social_login(request, login)

I did some digging and I think it's somehow connected to the latest update in 0.55.2 django-allauth in a property that the mentioned function uses:

def complete_social_login(request, sociallogin):
    assert not sociallogin.is_existing
    ...

This has changed from:

    @property
    def is_existing(self):
        """Account is temporary, not yet backed by a database record.
        """
        return self.account.pk is not None

to:

    @property
    def is_existing(self):
        """When `False`, this social login represents a temporary account, not
        yet backed by a database record.
        """
        return self.user.pk is not None
niko-chaffinchicas commented 9 months ago

@itsdkey What error are you getting exactly? Was it something like this?

save() prohibited to prevent data loss due to unsaved related object 'app'.

My teammate and I ran into some issues with the Google integration and ended up having to subclass the SocialLoginSerializer in order to make sure that the login endpoint for Google accepted a "refresh_token" as well as a "expires_in" property. Something we changed in that process seems to have addressed the above error, I'll try to dig more into it tomorrow.

itsdkey commented 9 months ago

@niko-chaffinchicas The error was an AssertionError with no message (because of the first statement in complete_social_login function).

niko-chaffinchicas commented 9 months ago

Oh I'm sorry, that's a different issue than I ran into. 😬

Aniket-Singla commented 8 months ago

+1

This is the django-allauth commit that made this change : https://github.com/pennersr/django-allauth/commit/6c10cda1fe35d3d275bdedc6bee1e3f382f54401

For now downgraded dj-rest-auth back to 4.0.1 as a resolution.

Aniket-Singla commented 8 months ago

Update on this: As a part of https://github.com/pennersr/django-allauth/issues/3446 , django-allauth has fixed this in 0.58.1 already. This might happen for applications that are using UUID as pk for user model.

Expected Resolution: After checking django-allauth changelog for 0.55.2 .. 0.58.1 and testing , explicitly release dj-rest-auth v5.0.1 with django-allauth 0.58.2

NTN-code commented 6 months ago

@Aniket-Singla Thanks you a lot(after spend approximately 7 hours )!!! It's really fixed =)

I have User model with UUID primary key and after restore default id field it start work

My example of using django-allauth and dj-rest-auth

users/views.py

class GoogleLoginRedirect(RedirectView):
    url = "https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=<REDIRECT_URL(WITH OUT SLASH IN THE END)>&prompt=consent&response_type=code&client_id=<CLIENT_ID>&scope=openid%20email%20profile&access_type=offline"
    permanent = True

    def get_redirect_url(self, *args, **kwargs):
        return self.url

import requests
class GoogleCallback(View):
    def get(self, request, *args, **kwargs):
        code = request.GET["code"]
        response = requests.post("http://localhost:8000/dj-rest-auth/google/", json={
            "code": code
        })

        django_response = HttpResponse(
            content=response.content,
            status=response.status_code,
        )
        for k, v in response.headers.items():
            django_response[k] = v

        return django_response

urls.py

    path('dj-rest-auth/google/', GoogleLogin.as_view(), name='google_login'),
    path("dj-rest-auth/google/login/", view=GoogleLoginRedirect.as_view(), name="google_redirect"),
    path("dj-rest-auth/google/callback/", view=GoogleCallback.as_view(), name="google_callback"),

UUID primary key not work for django-allauth = "~0.56.1" dj-rest-auth = "~5.0.2"

pennersr commented 6 months ago

You are using 0.56 -- there was an issue that was fixed in 0.57.0 (see https://github.com/pennersr/django-allauth/issues/3446#issuecomment-1732051830)

itsdkey commented 6 months ago

Great, it works! I think we can close this issue then.