Skip to content

Commit

Permalink
Merge pull request #7 from teamhashed:whoismaruf/issue6
Browse files Browse the repository at this point in the history
Set cookies when refresh token.
  • Loading branch information
whoismaruf authored Oct 17, 2022
2 parents ac0cfb8 + 42965fe commit b39798c
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions credentials/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit b39798c

Please sign in to comment.