Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework return user profile along with api token after login or registration

I've got the following serializer:

from rest_framework import serializers
from allauth.account import app_settings as allauth_settings
from allauth.utils import email_address_exists
from allauth.account.adapter import get_adapter
from allauth.account.utils import setup_user_email
from kofiapi.api.users.models import User, UserProfile

class UserProfileSerializer(serializers.ModelSerializer):

    class Meta:
        model = UserProfile
        fields = ('dob', 'phone', 'receive_newsletter')


class UserSerializer(serializers.HyperlinkedModelSerializer):

    profile = UserProfileSerializer(required=True)

    class Meta:
        model = User
        fields = ('url', 
                  'email', 
                  'first_name', 
                  'last_name',
                  'password',
                  'profile')
        extra_kwargs = {'password': {'write_only': True}}
        
    def create(self, validated_data):
        profile_data = validated_data.pop('profile')

        password = validated_data.pop('password')

        user = User(**validated_data)
        user.set_password(password)
        user.save()

        UserProfile.objects.create(user=user, **profile_data)

        return user

    def update(self, instance, validated_data):

        profile_data = validated_data.pop('profile')
        profile = instance.profile

        instance.email = validated_data.get('email', instance.name)
        instance.save()

        profile.dob = profile_data.get('dob', profile.dob)
        profile.phone = profile_data.get('phone', profile.phone)
        profile.receive_newsletter = profile_data.get('receive_newsletter', profile.receive_newsletter)
        profile.save()

        return instance

and these are the respective routes I have:

router = DefaultRouter()
router.register(r"users", UserViewSet)

urlpatterns = [
    path('', include(router.urls)),

    path('rest-auth/', include('rest_auth.urls')),
    #path('rest-auth/registration/', include('rest_auth.registration.urls')),
]

I'm using:

'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',

configured as:

AUTH_USER_MODEL = 'users.User'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
}

when I log in I'm getting back only the token key:

{
    "key": "8c0d808c0413932e478be8882b2ae829efa74b3e"
}

how can I make it to return user info along with the key upon registration/login? Something like:

{
    "key": "8c0d808c0413932e478be8882b2ae829efa74b3e",
    "user": {
           // user data
    }
}
like image 537
HerbertRedford Avatar asked Jan 25 '23 02:01

HerbertRedford


1 Answers

set TOKEN_SERIALIZER settings as,

#serializers.py
from rest_auth.serializers import TokenSerializer
from django.contrib.auth import get_user_model


class UserTokenSerializer(serializers.ModelSerializer):
    class Meta:
        model = get_user_model()
        fields = ('id', 'email')


class CustomTokenSerializer(TokenSerializer):
    user = UserTokenSerializer(read_only=True)

    class Meta(TokenSerializer.Meta):
        fields = ('key', 'user')


#settings.py
REST_AUTH_SERIALIZERS = {
    'TOKEN_SERIALIZER': 'path.to.custom.CustomTokenSerializer',

}

Reference

  • Rest Auth Configuration
like image 62
JPG Avatar answered Jan 31 '23 10:01

JPG