django-rest-auth icon indicating copy to clipboard operation
django-rest-auth copied to clipboard

Customize login view response

Open math77 opened this issue 5 years ago • 4 comments

Hello,

how can I customize the response of the login view so that it returns the token and also the id of the logged in user?

math77 avatar Jun 27 '19 23:06 math77

did you figure out a solution? im also looking for a way to return more info about the user

ethanyuwang avatar Aug 24 '20 01:08 ethanyuwang

Create custom view that inherits LoginView

from rest_auth.registration.views import LoginView

...

class LoginViewCustom(LoginView):
    authentication_classes = (TokenAuthentication,)

    def post(self, request, *args, **kwargs):
        response = super().post(request, *args, **kwargs)

        user_serializer_data = UserSerializer(self.user, context={'request': request})
        aaa = {'user': user_serializer_data.data}
        aaa.update(response.data)

        return Response(aaa)

mateoKutnjak avatar Aug 30 '20 20:08 mateoKutnjak

Ohh, thanks! That works for me.

math77 avatar Sep 17 '20 13:09 math77

It's working fine. Great work. @mateoKutnjak