|
1 | 1 | import uuid |
2 | 2 |
|
3 | | -from allauth.account.views import ConfirmEmailView |
4 | | -from allauth.account.utils import complete_signup |
| 3 | +from allauth.account.forms import UserTokenForm |
| 4 | +from allauth.account.utils import complete_signup, url_str_to_user_pk |
5 | 5 | from allauth.account.internal import flows |
6 | 6 | from allauth.account.models import ( |
7 | 7 | get_emailconfirmation_model, |
| 8 | + EmailAddress |
8 | 9 | ) |
9 | 10 |
|
10 | 11 | from django.http import Http404 |
|
16 | 17 | from rest_framework.mixins import RetrieveModelMixin |
17 | 18 | from rest_framework.mixins import UpdateModelMixin |
18 | 19 | from rest_framework.viewsets import GenericViewSet |
19 | | -from rest_framework.decorators import action |
| 20 | +from rest_framework.decorators import action, renderer_classes |
20 | 21 | from rest_framework.response import Response |
21 | 22 | from rest_framework.views import APIView |
22 | 23 | from rest_framework import status, permissions |
23 | 24 | from rest_framework_simplejwt.tokens import RefreshToken |
24 | 25 |
|
25 | 26 | from users.models import User |
26 | | -from users.api.serializers import UserSerializer, RegisterSerializer |
| 27 | +from users.api.serializers import UserSerializer, RegisterSerializer, PasswordResetSerializer, PasswordResetConfirmSerializer |
27 | 28 | from config.settings.base import CONFIRM_EMAIL_ON_GET |
28 | 29 |
|
29 | 30 | class UserViewSet(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, GenericViewSet): |
@@ -95,6 +96,71 @@ def get(self, *args, **kwargs): |
95 | 96 | return Response({"detail": "Not accepting GET request on the endpoint"}, status=status.HTTP_400_BAD_REQUEST) |
96 | 97 |
|
97 | 98 |
|
| 99 | +class CustomPasswordResetFromKeyView(APIView): |
| 100 | + permission_classes = [permissions.AllowAny] |
| 101 | + |
| 102 | + def get_user(self, uidb36): |
| 103 | + try: |
| 104 | + uid_int = url_str_to_user_pk(uidb36) |
| 105 | + user = User.objects.get(pk=uid_int) |
| 106 | + if not user: |
| 107 | + return None |
| 108 | + return user |
| 109 | + except(ValueError, User.DoesNotExist): |
| 110 | + return Response({"detail": "Invalid user ID."}) |
| 111 | + |
| 112 | + def get(self, request, uidb36, key, *args, **kwargs): |
| 113 | + # Check if the reset link is valid |
| 114 | + user = self.get_user(uidb36) |
| 115 | + if not user: |
| 116 | + return Response( |
| 117 | + {"detail": "Invalid reset link."}, |
| 118 | + status=status.HTTP_400_BAD_REQUEST, |
| 119 | + ) |
| 120 | + |
| 121 | + # Return a response indicating the reset link is valid |
| 122 | + return Response( |
| 123 | + {"detail": "Please submit your new password."}, |
| 124 | + status=status.HTTP_200_OK, |
| 125 | + ) |
| 126 | + |
| 127 | + def post(self, request, uidb36, key): |
| 128 | + serializer = PasswordResetConfirmSerializer(data=request.data) |
| 129 | + |
| 130 | + if serializer.is_valid(raise_exception=True): |
| 131 | + # Decode the user ID |
| 132 | + user = self.get_user(uidb36) |
| 133 | + |
| 134 | + # Validate the reset key. |
| 135 | + token_form = UserTokenForm(data={'uidb36': uidb36, 'key': key}) |
| 136 | + if not token_form.is_valid(): |
| 137 | + return Response({"detail": "Invalid or expired reset key."}, status=status.HTTP_400_BAD_REQUEST) |
| 138 | + |
| 139 | + # Ensure the user email is verified. |
| 140 | + if not EmailAddress.objects.filter(user=user, verified=True).exists(): |
| 141 | + return Response({"detail": "Email address not verified."}, status=status.HTTP_400_BAD_REQUEST) |
| 142 | + |
| 143 | + # Get the new password from the request |
| 144 | + new_password = request.data.get("new_password1") |
| 145 | + |
| 146 | + # Set the new password |
| 147 | + user.set_password(new_password) |
| 148 | + user.save() |
| 149 | + return Response({"detail": "Password has been reset successfully"}, status=status.HTTP_200_OK) |
| 150 | + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 151 | + |
| 152 | +class PasswordResetView(APIView): |
| 153 | + permission_classes = [permissions.AllowAny] |
| 154 | + |
| 155 | + def post(self, request): |
| 156 | + serializer = PasswordResetSerializer(data=request.data, context={'request': request}) |
| 157 | + |
| 158 | + if serializer.is_valid(): |
| 159 | + serializer.save() |
| 160 | + return Response({"detail": "Password reset email has been sent."}, status=status.HTTP_200_OK) |
| 161 | + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 162 | + |
| 163 | + |
98 | 164 | class UserProfileView(APIView): |
99 | 165 | permission_classes = [permissions.IsAuthenticated] |
100 | 166 |
|
|
0 commit comments