From b4bacbe35af7bcc51f9d5a9e8fc4dc1a1bf54301 Mon Sep 17 00:00:00 2001 From: bobbyvish Date: Mon, 13 May 2024 20:05:17 +0530 Subject: [PATCH] refactor(auth):changed logout functionality --- module_auth/api/urls.py | 1 + module_auth/api/utils.py | 11 ++++++++++- module_auth/api/views.py | 21 ++++++++++++++++++++- module_project/settings/base.py | 1 + 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/module_auth/api/urls.py b/module_auth/api/urls.py index f943791..1dada83 100644 --- a/module_auth/api/urls.py +++ b/module_auth/api/urls.py @@ -7,6 +7,7 @@ urlpatterns = [ path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), path("signup/", views.RegistrationView.as_view()), path("login/", views.LoginView.as_view()), + path("logout/", views.LogoutView.as_view()), path("request-otp/", views.OtpRequestView.as_view()), path("verify-otp/", views.OTPVerificationView.as_view()), diff --git a/module_auth/api/utils.py b/module_auth/api/utils.py index 9e77ed3..1d92138 100644 --- a/module_auth/api/utils.py +++ b/module_auth/api/utils.py @@ -3,7 +3,8 @@ from typing import Optional import requests from django.core.exceptions import ValidationError -from rest_framework_simplejwt.tokens import RefreshToken +from rest_framework_simplejwt.tokens import RefreshToken, TokenError +from rest_framework_simplejwt.exceptions import TokenError from module_iam.models import IAmPrincipal, IAmPrincipalOtp from module_project import constants @@ -30,6 +31,14 @@ def generate_token_and_user_data(principal): } return data +def blacklist_token(token): + try: + RefreshToken(token).blacklist() + print("token is blacklisted") + except TokenError: + print("error occurs") + pass + class GoogleAuthService(): @staticmethod def get_user_info(access_token): diff --git a/module_auth/api/views.py b/module_auth/api/views.py index 0581131..01bb55b 100644 --- a/module_auth/api/views.py +++ b/module_auth/api/views.py @@ -18,7 +18,7 @@ from module_project.utils import ApiResponse from .serializers import (LoginSerializer, OtpVerificationSerializer, PasswordResetSerializer, RegistrationSerializer) from .utils import (AuthService, GoogleAuthService, - authticate_with_otp_and_passsword, + authticate_with_otp_and_passsword, blacklist_token, generate_token_and_user_data, get_principal_by_email) @@ -111,6 +111,25 @@ class LoginView(APIView): return ApiResponse.success(message=constants.LOGIN_SUCCESS, data=token_data) +class LogoutView(APIView): + authentication_classes = [JWTAuthentication] + permission_classes = [IsAuthenticated] + model = IAmPrincipal + + def post(self, request): + token = request.data.get("refresh") + if not token: + return ApiResponse.error(message=constants.FAILURE, errors='Provide refresh token') + + user = request.user + user.player_id = None + user.save() + + blacklist_token(token) + + return ApiResponse.success(message=constants.LOGOUT_SUCCESS) + + class OtpRequestView(APIView): authentication_classes = [] permission_classes = [] diff --git a/module_project/settings/base.py b/module_project/settings/base.py index ef5b9e8..84e5e8f 100644 --- a/module_project/settings/base.py +++ b/module_project/settings/base.py @@ -60,6 +60,7 @@ THIRD_PARTY_APPS = [ "corsheaders", "widget_tweaks", "rest_framework_simplejwt", + 'rest_framework_simplejwt.token_blacklist', "taggit", "django_quill", "django_crontab",