Skip to content

Commit

Permalink
feat: add cookie authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
sterapps committed Oct 15, 2023
1 parent fc7796f commit 3f3d229
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion drf_anonymous_login/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

AUTH_KEYWORD = "Token"
AUTH_HEADER = "HTTP_X_AUTHORIZATION_ANONYMOUS"
AUTH_COOKIE = "anonymous_token"


class AnonymousLoginAuthentication(authentication.BaseAuthentication):
keyword = AUTH_KEYWORD

def authenticate(self, request):
auth = request.META.get(AUTH_HEADER, "").split()
auth = request.META.get(AUTH_HEADER, "").split() or request.COOKIES.get(AUTH_COOKIE, "").split()

if not auth or auth[0].lower() != self.keyword.lower():
return None
Expand Down
4 changes: 3 additions & 1 deletion drf_anonymous_login/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def create(self, request, *args, **kwargs):
"headers": self.extract_request_headers(request),
}
)
return Response({"token": user.token}, status=status.HTTP_201_CREATED)
response = Response({"token": user.token}, status=status.HTTP_201_CREATED)
response.set_cookie("anonymous_token", user.token)
return response


class AnonymousLoginAuthenticationModelViewSet(viewsets.ModelViewSet):
Expand Down

0 comments on commit 3f3d229

Please sign in to comment.