Closed ghost closed 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:
- Define your user serializer, add optional
is_new
fieldfrom 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
- Specify it in the view
from rest_social_auth.views import SocialSessionAuthView from .serializers import MyUserSerializer class MySocialView(SocialSessionAuthView): serializer_class = MyUserSerializer
- 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
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:
is_new
fieldclass MyUserSerializer(serializers.ModelSerializer): is_new = serializers.BooleanField(required=False, read_only=True, default=False)
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.