st4lk / django-rest-social-auth

OAuth signin with django rest framework
MIT License
519 stars 122 forks source link

Is there a way for the client to know if the oauth2 flow resulted in a signup or a signin? #177

Closed ghost closed 7 months ago

st4lk commented 7 months ago

Good question. Yes, there is a way. During auth, social-core is adding 'is_new' bool attribute to the user instance. So you can add it to your serializer.

Let's say you are using session to authenticate the user. Then you can override the serializer like this:

  1. Define your user serializer, add optional is_new field
    
    from rest_framework import serializers
    from django.contrib.auth import get_user_model

class MyUserSerializer(serializers.ModelSerializer): is_new = serializers.BooleanField(required=False, read_only=True, default=False)

class Meta:
    model = get_user_model()
    fields = '__all__'  # or whatever fields you need
2. Specify it in the view
```python
from rest_social_auth.views import SocialSessionAuthView
from .serializers import MyUserSerializer

class MySocialView(SocialSessionAuthView):
    serializer_class = MyUserSerializer
  1. Override the url:
    urlpatterns = [
    re_path(
        r'^api/login/social/session/(?:(?P<provider>[a-zA-Z0-9_-]+)/?)?$',
        views.MySocialView.as_view(),
        name='social_login',
    ),
    ...
    ]

    After that you should seen either {"is_new": False, ...} in response - it means signin OR {"is_new": True, ...} - it means signup.

For other ways to authenticate the user (Token, JWT, etc) it can be done in similar way. Worth adding to the base functionality of current package btw.

mahiuddin-dev commented 7 months ago

Good question. Yes, there is a way. During auth, social-core is adding 'is_new' bool attribute to the user instance. So you can add it to your serializer.

Let's say you are using session to authenticate the user. Then you can override the serializer like this:

  1. Define your user serializer, add optional is_new field
from rest_framework import serializers
from django.contrib.auth import get_user_model

class MyUserSerializer(serializers.ModelSerializer):
    is_new = serializers.BooleanField(required=False, read_only=True, default=False)

    class Meta:
        model = get_user_model()
        fields = '__all__'  # or whatever fields you need
  1. Specify it in the view
from rest_social_auth.views import SocialSessionAuthView
from .serializers import MyUserSerializer

class MySocialView(SocialSessionAuthView):
    serializer_class = MyUserSerializer
  1. Override the url:
urlpatterns = [
    re_path(
        r'^api/login/social/session/(?:(?P<provider>[a-zA-Z0-9_-]+)/?)?$',
        views.MySocialView.as_view(),
        name='social_login',
    ),
    ...
]

After that you should seen either {"is_new": False, ...} in response - it means signin OR {"is_new": True, ...} - it means signup.

For other ways to authenticate the user (Token, JWT, etc) it can be done in similar way. Worth adding to the base functionality of current package btw.

Thanks