From 42965fe883ddd5b31ac1dd0bb0b3c6a5a706e39f Mon Sep 17 00:00:00 2001 From: Maruf Khan Date: Tue, 18 Oct 2022 00:14:42 +0600 Subject: [PATCH] Set cookies when refresh token. Fixes #6 --- credentials/api/views.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/credentials/api/views.py b/credentials/api/views.py index 438d192..a1b4ad1 100644 --- a/credentials/api/views.py +++ b/credentials/api/views.py @@ -2,16 +2,28 @@ from django.http import JsonResponse from credentials.api.serializers import CustomUserDetailSerializer from django.contrib.auth import get_user_model +from dj_rest_auth.jwt_auth import set_jwt_cookies def get_tokens_for_user(request): - refresh = RefreshToken.for_user(request.user) - print(f'token for {request.user}',refresh.access_token) - q__set = get_user_model().objects.filter(email=request.user.email).first() - user_detil = CustomUserDetailSerializer(q__set) - return JsonResponse({ - 'refresh_token': str(refresh), - 'access_token': str(refresh.access_token), - 'user': user_detil.data - }) - + try: + refresh = RefreshToken.for_user(request.user) + print(request.user) + q__set = get_user_model().objects.filter(email=request.user.email).first() + user_detail = CustomUserDetailSerializer(q__set) + data = { + 'refresh_token': str(refresh), + 'access_token': str(refresh.access_token), + 'user': user_detail.data + } + res = JsonResponse(data) + set_jwt_cookies(response=res, + access_token=str(refresh.access_token), + refresh_token=str(refresh) + ) + return res + except Exception as e: + return JsonResponse({ + 'status': 404, + 'message': str(e) + })