Tuesday, 2 January 2024

django TokenAuthentication return object

 https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication

https://stackoverflow.com/questions/66271811/get-the-value-of-the-token-from-authentication-in-django


from rest_framework import generics, permissions

# Custom authentication class

from core import authentication

from core.models import Token

@csrf_exempt

@api_view(['GET'])

@authentication_classes([authentication.TokenAuthentication])

@permission_classes([permissions.IsAuthenticated])

def myapifunc(request):

    # request.user returns user obj and request.auth returns token obje

    userObj = request.user

    tokenObj = request.auth

    token = request.auth.key

    userName = request.user.name

No comments:

Post a Comment