Skip to content

Commit

Permalink
change refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiacampos98 committed Nov 10, 2024
1 parent 608a16f commit 5459768
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion backend/pong/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.3 on 2024-11-10 09:07
# Generated by Django 5.1.3 on 2024-11-10 12:04

import django.db.models.deletion
from django.conf import settings
Expand Down
64 changes: 36 additions & 28 deletions backend/pong/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,43 @@ def refresh_access_token(refresh_token):
return None


@api_view(['GET'])
def check_token(request):
jwt_authenticator = JWTAuthentication()
token_valid = None

# Step 1: Attempt to authenticate the token
try:
auth_result = jwt_authenticator.authenticate(request)
if auth_result:
user, validated_token = auth_result
token_valid = True
except Exception as e:
# Token is likely invalid
token_valid = False

# Step 2: Refresh the token if invalid
if not token_valid:
refresh_token_value = request.COOKIES.get('refresh_token')

if refresh_token_value:
refresh = RefreshToken(refresh_token_value)
new_access_token = str(refresh.access_token)
response_data = {'access_token': new_access_token}
return Response(response_data)
else:
return Response({'error': 'Refresh token not found or expired.'}, status=401)

# Step 3: If the token is valid, return a confirmation response
return Response({'message': 'The token is valid.'})
if request.method == 'GET':
ic("check")
jwt_authenticator = JWTAuthentication()
token_valid = None

# Step 1: Attempt to authenticate the token
try:
auth_result = jwt_authenticator.authenticate(request)
if auth_result:
user, validated_token = auth_result
token_valid = True
except Exception as e:
# Token is likely invalid
token_valid = False
ic(token_valid)
# Step 2: Refresh the token if invalid
if not token_valid:
ic(token_valid)
ic("aqui1")
refresh_token_value = request.COOKIES.get('refresh_token')

if refresh_token_value:
try:
ic("aqui")
refresh = RefreshToken(refresh_token_value)
new_access_token = str(refresh.access_token)
response_data = {'access_token': new_access_token}
return JsonResponse(response_data, status=200)
except Exception as e:
return JsonResponse({'message': 'Failed to refresh token.'}, status=401)
else:
return JsonResponse({'message': 'Refresh token not found or expired.'}, status=401)

# Step 3: If the token is valid, return a confirmation response
return JsonResponse({'message': 'The token is valid.'}, status=200)
return JsonResponse({'message': 'Invalid request method.', 'method': request.method}, status=405)

#! --------------------------------------- Users ---------------------------------------

Expand Down

0 comments on commit 5459768

Please sign in to comment.