I've got a backend authentication using django rest framework simplejwt When I make request to it in angular I notice that 2 requests are made instead of one, for example you can look at this:
Unauthorized: /courses/project/get/lessons
Unauthorized: /courses/project/get/lessondata/
[15/Mar/2025 07:46:58] "GET /courses/project/get/lessons?id=7&chapter_id=16&unit_id=12 HTTP/1.1" 401 58
[15/Mar/2025 07:46:58] "POST /courses/project/get/lessondata/ HTTP/1.1" 401 58
RefreshToken: None
Ip adress: 127.0.0.1
Bad Request: /users/token/refresh/
[15/Mar/2025 07:46:59] "POST /users/token/refresh/ HTTP/1.1" 400 40
[15/Mar/2025 07:46:59] "GET /courses/project/get/lessons?id=7&chapter_id=16&unit_id=12 HTTP/1.1" 200 110
[15/Mar/2025 07:46:59] "POST /courses/project/get/lessondata/ HTTP/1.1" 200 1523
[15/Mar/2025 07:46:59] "GET /users/verify/ HTTP/1.1" 200 32
To proof it, I made the http interceptor print out each request and got this in the
console: Request sent to: http://127.0.0.1:8000/courses/project/get/lessons
auth.interceptor.ts:20 Request sent to: http://127.0.0.1:8000/courses/project/get/lessondata/
appponent.ts:39 Access token exists, verifying user...
auth.interceptor.ts:20 Request sent to: http://127.0.0.1:8000/users/verify/
Also here is my refresh token view:
def post(self, request, *args, **kwargs):
# Extract refresh token from HttpOnly cookie
refresh_token = request.COOKIES.get('refresh_token')
print(f"RefreshToken: {refresh_token}")
print(f"Ip adress: {get_client_ip(request)}")
if not refresh_token:
raise ValidationError({"refresh": ["Refresh token is missing"]})
# Manually pass the refresh token to the serializer
serializer = TokenRefreshSerializer(data={"refresh": refresh_token})
serializer.is_valid(raise_exception=True)
# Return the new access token
return Response({
"access": serializer.validated_data['access'],
})
And here's a sample view for one of the requests (courses/project/getlessondata/)
class GetLessonData(APIView):
permission_classes = [IsAuthenticated]
def post(self, request):
user = request.user
if ChapterLesson.objects.filter(id=request.data.get("id")).exists():
lesson = ChapterLesson.objects.get(id=request.data.get("id"))
if ProjectUser.objects.filter(user=user, course=lesson.unit.chapter.course).exists():
file_field = lesson.file
if file_field:
with open(file_field.path, mode="rb") as file:
content = file.read().decode("utf-8")
return Response({"state": True, "content": content})
return Response({"state": False, "reason": "No file found"}, status=HTTP_404_NOT_FOUND)
return Response({"state": False, "reason": "Unauthenticated approach, don't try again!"}, status=HTTP_401_UNAUTHORIZED)
return Response({"state": False, "reason": "No valid lesson"}, status=HTTP_404_NOT_FOUND)
I still don't understand what's going on, I'd appreciate any help
I tried a bunch of different things, one of which was printing out each refresh token recieved, when the user refreshes the access token using the refresh token, django rest framework says that there were two requests despite me only making a single request in angular. One the first request (the one which gives the error) I recieved that the refresh token was None. And on the second request (the correct one) which happens about 1 second after the original, I recieve a valid refresh token.