mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Merge pull request #447 from rrakso/feature/add_method_to_delete_token
Feature/add method to delete token
This commit is contained in:
commit
daa5a32440
@ -1,10 +1,12 @@
|
|||||||
from rest_framework import generics, permissions
|
from rest_framework import generics, permissions
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from .serializers import UserSerializer
|
from .serializers import UserSerializer
|
||||||
|
|
||||||
from rest_framework.authtoken.views import ObtainAuthToken
|
from rest_framework.authtoken.views import ObtainAuthToken
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from rest_framework import status
|
||||||
|
|
||||||
|
|
||||||
class UserDetail(generics.RetrieveAPIView):
|
class UserDetail(generics.RetrieveAPIView):
|
||||||
@ -27,6 +29,12 @@ class GetAuthToken(ObtainAuthToken):
|
|||||||
""" Return authentication token for an authenticated user. """
|
""" Return authentication token for an authenticated user. """
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
|
return self.login(request)
|
||||||
|
|
||||||
|
def delete(self, request):
|
||||||
|
return self.logout(request)
|
||||||
|
|
||||||
|
def login(self, request):
|
||||||
serializer = self.serializer_class(data=request.data,
|
serializer = self.serializer_class(data=request.data,
|
||||||
context={'request': request})
|
context={'request': request})
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
@ -39,3 +47,12 @@ class GetAuthToken(ObtainAuthToken):
|
|||||||
'username': user.username,
|
'username': user.username,
|
||||||
'email': user.email
|
'email': user.email
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def logout(self, request):
|
||||||
|
try:
|
||||||
|
request.user.auth_token.delete()
|
||||||
|
return Response({"success": "Successfully logged out."},
|
||||||
|
status=status.HTTP_202_ACCEPTED)
|
||||||
|
except (AttributeError, ObjectDoesNotExist):
|
||||||
|
return Response({"error": "Bad request"},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
Loading…
Reference in New Issue
Block a user