|
1 | 1 | from logging import getLogger |
2 | 2 |
|
| 3 | +from django.shortcuts import render |
| 4 | +from django.views import View |
3 | 5 | from djoser.views import UserViewSet as BaseUserViewSet |
4 | 6 | from drf_spectacular.utils import OpenApiResponse, extend_schema |
5 | 7 | from rest_framework import status |
6 | 8 | from rest_framework.decorators import action |
7 | 9 | from rest_framework.response import Response |
| 10 | +from rest_framework_simplejwt.tokens import RefreshToken |
| 11 | +from rest_framework_simplejwt.views import ( |
| 12 | + TokenObtainPairView as BaseTokenObtainPairView, |
| 13 | + TokenRefreshView as BaseTokenRefreshView, |
| 14 | + TokenVerifyView as BaseTokenVerifyView, TokenBlacklistView, |
| 15 | +) |
8 | 16 |
|
9 | 17 | from .models import Profile |
10 | | -from .serializers import UserProfileSerializer |
| 18 | +from .serializers import UserProfileSerializer, RefreshTokenSerializer |
11 | 19 |
|
12 | 20 | logger = getLogger(__name__) |
13 | 21 |
|
@@ -243,3 +251,124 @@ def staff_check(self, request): |
243 | 251 | return Response({ |
244 | 252 | "error": "Unable to check staff status" |
245 | 253 | }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |
| 254 | + |
| 255 | + |
| 256 | +class TokenObtainPairView(BaseTokenObtainPairView): |
| 257 | + """ |
| 258 | + Handle POST requests to obtain a new pair of access and refresh tokens. |
| 259 | + """ |
| 260 | + |
| 261 | + @extend_schema( |
| 262 | + operation_id="token_obtain", |
| 263 | + description="Obtain a new pair of access and refresh tokens.", |
| 264 | + tags=["User Authentication"], |
| 265 | + responses={ |
| 266 | + 200: OpenApiResponse(description="Token successfully obtained."), |
| 267 | + 400: OpenApiResponse(description="Invalid credentials."), |
| 268 | + } |
| 269 | + ) |
| 270 | + def post(self, request, *args, **kwargs): |
| 271 | + try: |
| 272 | + response = super().post(request, *args, **kwargs) |
| 273 | + return Response({ |
| 274 | + "message": "Token successfully obtained", |
| 275 | + "data": response.data |
| 276 | + }, status=response.status_code) |
| 277 | + except Exception as e: |
| 278 | + logger.error(f"Error during token obtain: {e}", exc_info=True) |
| 279 | + raise |
| 280 | + |
| 281 | + |
| 282 | +class TokenRefreshView(BaseTokenRefreshView): |
| 283 | + """ |
| 284 | + Handle POST requests to refresh an access token using a refresh token. |
| 285 | + """ |
| 286 | + |
| 287 | + @extend_schema( |
| 288 | + operation_id="token_refresh", |
| 289 | + description="Refresh an access token using a refresh token.", |
| 290 | + tags=["User Authentication"], |
| 291 | + responses={ |
| 292 | + 200: OpenApiResponse(description="Access token successfully refreshed."), |
| 293 | + 400: OpenApiResponse(description="Invalid refresh token."), |
| 294 | + } |
| 295 | + ) |
| 296 | + def post(self, request, *args, **kwargs): |
| 297 | + try: |
| 298 | + response = super().post(request, *args, **kwargs) |
| 299 | + return Response({ |
| 300 | + "message": "Access token successfully refreshed", |
| 301 | + "data": response.data |
| 302 | + }, status=response.status_code) |
| 303 | + except Exception as e: |
| 304 | + logger.error(f"Error during token refresh: {e}", exc_info=True) |
| 305 | + raise |
| 306 | + |
| 307 | + |
| 308 | +class TokenVerifyView(BaseTokenVerifyView): |
| 309 | + """ |
| 310 | + Verify if an access token is valid. |
| 311 | + """ |
| 312 | + |
| 313 | + @extend_schema( |
| 314 | + operation_id="token_verify", |
| 315 | + description="Verify if an access token is valid.", |
| 316 | + tags=["User Authentication"], |
| 317 | + responses={ |
| 318 | + 200: OpenApiResponse(description="Token is valid."), |
| 319 | + 401: OpenApiResponse(description="Token is invalid or expired."), |
| 320 | + } |
| 321 | + ) |
| 322 | + def post(self, request, *args, **kwargs): |
| 323 | + try: |
| 324 | + response = super().post(request, *args, **kwargs) |
| 325 | + return Response({ |
| 326 | + "message": "Token is valid", |
| 327 | + "data": response.data |
| 328 | + }, status=response.status_code) |
| 329 | + except Exception as e: |
| 330 | + logger.error(f"Error during token verification: {e}", exc_info=True) |
| 331 | + raise |
| 332 | + |
| 333 | + |
| 334 | +class TokenDestroyView(TokenBlacklistView): |
| 335 | + """ |
| 336 | + Log out the user by blacklisting their refresh token. |
| 337 | + """ |
| 338 | + serializer_class = RefreshTokenSerializer |
| 339 | + |
| 340 | + @extend_schema( |
| 341 | + operation_id="logout_user", |
| 342 | + description="Log out the user by blacklisting their refresh token.", |
| 343 | + tags=["User Authentication"], |
| 344 | + request=RefreshTokenSerializer, |
| 345 | + responses={ |
| 346 | + 205: OpenApiResponse(description="Successfully logged out"), |
| 347 | + 400: OpenApiResponse(description="Invalid Token"), |
| 348 | + }, |
| 349 | + ) |
| 350 | + def post(self, request, *args, **kwargs): |
| 351 | + serializer = self.get_serializer(data=request.data) |
| 352 | + try: |
| 353 | + serializer.is_valid(raise_exception=True) |
| 354 | + refresh_token = serializer.validated_data["refresh"] |
| 355 | + token = RefreshToken(refresh_token) |
| 356 | + token.blacklist() |
| 357 | + return Response({ |
| 358 | + "message": "Successfully logged out" |
| 359 | + }, status=status.HTTP_205_RESET_CONTENT) |
| 360 | + except Exception as e: |
| 361 | + logger.error(f"Error during logout: {e}", exc_info=True) |
| 362 | + raise |
| 363 | + |
| 364 | + |
| 365 | +class ActivateView(View): |
| 366 | + def get(self, request, uid, token): |
| 367 | + return render( |
| 368 | + request, |
| 369 | + 'account/activate.html', |
| 370 | + { |
| 371 | + 'uid': uid, |
| 372 | + 'token': token, |
| 373 | + } |
| 374 | + ) |
0 commit comments