Created
August 8, 2021 07:44
-
-
Save labohkip81/e7ad366d3a83ab618f25dfdaf51f0ed9 to your computer and use it in GitHub Desktop.
Return username and token in django rest framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework.authtoken.views import ObtainAuthToken | |
class CustomAuthToken(ObtainAuthToken): | |
def post(self, request, *args, **kwargs): | |
serializer = self.serializer_class( | |
data=request.data, context={"request": request} | |
) | |
serializer.is_valid(raise_exception=True) | |
user = serializer.validated_data["user"] | |
token, created = Token.objects.get_or_create(user=user) | |
return Response( | |
{ | |
"token": token.key, | |
"user_id": user.pk, | |
"email": user.email, | |
"username": user.username, | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment