[Fixed]-Django-rest-auth: social login with google

2👍

https://github.com/st4lk/django-rest-social-auth

class SocialLoginSignup(SocialSessionAuthView):
"""
    Mobile user social signup and login api view

    args:
        provider: name of the social network
        access_token: auth token got from the social sites
"""
serializer_class = SocialSignUpSerializer
authentication_classes = (TokenAuthentication,)

def post(self, request, *args, **kwargs):
    serializer = self.serializer_class(data=request.data)
    serializer.is_valid(raise_exception=True)
    provider_name = serializer.validated_data['provider']

    decorate_request(request, provider_name) # assign the provider class object in request

    authed_user = request.user if not request.user.is_anonymous() else None

    token = serializer.validated_data['access_token']

    if self.oauth_v1() and request.backend.OAUTH_TOKEN_PARAMETER_NAME not in serializer.validated_data:
        request_token = parse_qs(request.backend.set_unauthorized_token())
        return Response(request_token)

    try:
        # authentication function get call from assign class object in request
        user = request.backend.do_auth(token, user=authed_user)
    except social_exceptions.AuthException as e:
        raise exceptions.ParseError({'error':str(e)})
    except social_exceptions.AuthTokenError as e:
        raise exceptions.ParseError({'error': str(e)})
    except social_exceptions.AuthAlreadyAssociated as e:
        raise exceptions.ParseError({'error': str(e)})
    except social_exceptions.AuthFailed as e:
        raise exceptions.ParseError({'error':str(e)})
    except social_exceptions.AuthUnknownError as e:
        raise exceptions.ParseError({'error': str(e)})
    except social_exceptions.WrongBackend as e:
        raise exceptions.ParseError({'error':str(e)})
    except Exception as e:
        raise exceptions.ParseError({'error': social_message.INVALID_AUTH_TOKEN})

    token, created = Token.objects.get_or_create(user=user)
    return Response({'auth_token':token.key})

Leave a comment