Skip to content

Commit

Permalink
feat(token_service): add user ID validation and token refresh logic
Browse files Browse the repository at this point in the history
- Added validation to ensure user ID is provided in the request.
- Implemented retrieval of  using the provided user ID.
- Handled errors for missing user or token data.
- Updated token data after refreshing access token.
  • Loading branch information
Pooria Toof authored and Pooria Toof committed Aug 22, 2024
1 parent 0890e1a commit 934c203
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Backend/token_service/token_service/token_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,19 @@ def post(self, request, *args, **kwargs) -> Response:
{"error": "Refresh token is required"}, status=status.HTTP_400_BAD_REQUEST
)
try:
user_id = request.data.get("id")
if not user_id:
return Response({"error": "User id is required"}, status=status.HTTP_400_BAD_REQUEST)
user_object = UserTokens.objects.filter(id=user_id)
if not user_object:
return Response({"error": "User not found"}, status=status.HTTP_404_NOT_FOUND)
token_data = user_object.token_data
refresh_token = bearer.split(' ')[1]
refresh = RefreshToken(refresh_token)
access_token = str(refresh.access_token)
token_data["access"] = str(refresh.access_token)
user_object.token_data=token_data
user_object.save()
return Response({"access": access_token}, status=status.HTTP_200_OK)
except Exception as err:
return Response({"error": "Could not generate access token", "details": str(err)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Expand Down

0 comments on commit 934c203

Please sign in to comment.