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 20, 2023
1 parent fc7796f commit 3e9bb3b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 5 additions & 1 deletion drf_anonymous_login/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

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 3e9bb3b

Please sign in to comment.