Skip to content

Commit

Permalink
Add Token based auth
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshhari committed Feb 28, 2022
1 parent d4a9888 commit edc4468
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions care/users/api/viewsets/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from rest_framework import filters as drf_filters
from rest_framework import filters as rest_framework_filters
from rest_framework import mixins, status
from rest_framework.authtoken.models import Token
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
Expand Down Expand Up @@ -115,6 +116,20 @@ def getcurrentuser(self, request):
status=status.HTTP_200_OK, data=UserSerializer(request.user, context={"request": request}).data,
)

@action(detail=False, methods=["GET"])
def get_token(self, request):
if not request.user.is_authenticated:
raise PermissionError
token, _ = Token.objects.get_or_create(user=request.user)
return Response(status=status.HTTP_200_OK, data={"token": token.key})

@action(detail=False, methods=["GET"])
def delete_token(self, request):
if not request.user.is_authenticated:
raise PermissionError
Token.objects.filter(user=request.user).delete()
return Response(status=status.HTTP_204_NO_CONTENT)

def destroy(self, request, *args, **kwargs):
queryset = self.queryset
username = kwargs["username"]
Expand Down
2 changes: 2 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"allauth.socialaccount",
"storages",
"rest_framework",
"rest_framework.authtoken",
"drf_yasg",
"drf_extra_fields",
"location_field.apps.DefaultConfig",
Expand Down Expand Up @@ -308,6 +309,7 @@
"config.authentication.CustomJWTAuthentication",
"config.authentication.CustomBasicAuthentication",
"rest_framework.authentication.SessionAuthentication",
"rest_framework.authentication.TokenAuthentication",
),
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"PAGE_SIZE": 14,
Expand Down

0 comments on commit edc4468

Please sign in to comment.