mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 00:24:54 +00:00
forgot, reset and change password
This commit is contained in:
@ -36,8 +36,34 @@ class ResetPasswordSerializer(serializers.Serializer):
|
|||||||
user_details.set_password(password)
|
user_details.set_password(password)
|
||||||
user_details.save()
|
user_details.save()
|
||||||
return {'password':password}
|
return {'password':password}
|
||||||
return user_opt_details
|
return user_opt_details
|
||||||
|
return ''
|
||||||
|
|
||||||
|
class ChangePasswordSerializer(serializers.Serializer):
|
||||||
|
"""Update Password after verification"""
|
||||||
|
current_password = serializers.CharField(max_length=100)
|
||||||
|
new_password = serializers.CharField(required=True)
|
||||||
|
class Meta(object):
|
||||||
|
"""Meta info"""
|
||||||
|
model = User
|
||||||
|
|
||||||
|
def validate_current_password(self, value):
|
||||||
|
user = self.context
|
||||||
|
if self.context.password not in ('', None):
|
||||||
|
if user.check_password(value):
|
||||||
|
return value
|
||||||
|
raise serializers.ValidationError({"error":"Invalid Current password"})
|
||||||
|
def create(self, validated_data):
|
||||||
|
new_password = validated_data.pop('new_password')
|
||||||
|
user_details = User.objects.filter(email=self.context).last()
|
||||||
|
print("user_details==>", user_details)
|
||||||
|
if user_details:
|
||||||
|
print("333333333==>",user_details.password)
|
||||||
|
user_details.set_password(new_password)
|
||||||
|
user_details.save()
|
||||||
|
return {'password':new_password}
|
||||||
|
return user_details
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
{% extends "templated_email/email_base.email" %}
|
{% extends "templated_email/email_base.email" %}
|
||||||
|
|
||||||
{% block subject %}
|
{% block subject %}
|
||||||
Reset Password Verification
|
Password Reset Verification Code
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block plain %}
|
{% block plain %}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ from rest_framework.decorators import api_view
|
|||||||
"""Third party import"""
|
"""Third party import"""
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp,
|
from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp,
|
||||||
ForgotPasswordAPIView, ResetPasswordAPIView)
|
ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView)
|
||||||
"""Router"""
|
"""Router"""
|
||||||
router = routers.SimpleRouter()
|
router = routers.SimpleRouter()
|
||||||
|
|
||||||
@ -20,5 +20,6 @@ router.register('resend-email-otp', ReSendEmailOtp, basename='resend-email-otp')
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/v1/', include(router.urls)),
|
path('api/v1/', include(router.urls)),
|
||||||
path('api/v1/forgot-password/', ForgotPasswordAPIView.as_view()),
|
path('api/v1/forgot-password/', ForgotPasswordAPIView.as_view()),
|
||||||
path('api/v1/reset-password/', ResetPasswordAPIView.as_view())
|
path('api/v1/reset-password/', ResetPasswordAPIView.as_view()),
|
||||||
|
path('api/v1/change-password/', ChangePasswordAPIView.as_view())
|
||||||
]
|
]
|
||||||
|
|||||||
@ -7,21 +7,34 @@ from junior.models import Junior
|
|||||||
from account.models import UserProfile, UserPhoneOtp, UserEmailOtp
|
from account.models import UserProfile, UserPhoneOtp, UserEmailOtp
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer,
|
from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer,
|
||||||
ForgotPasswordSerializer, ResetPasswordSerializer)
|
ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer)
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
|
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
|
||||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||||
from rest_framework_simplejwt.tokens import RefreshToken
|
from rest_framework_simplejwt.tokens import RefreshToken
|
||||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||||
from guardian.tasks import generate_otp
|
from guardian.tasks import generate_otp
|
||||||
|
from django.conf import settings
|
||||||
from account.utils import custom_response, custom_error_response
|
from account.utils import custom_response, custom_error_response
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.core.mail import send_mail
|
from django.core.mail import send_mail
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from templated_email import send_templated_mail
|
from templated_email import send_templated_mail
|
||||||
import secrets
|
import secrets
|
||||||
|
|
||||||
|
|
||||||
|
class ChangePasswordAPIView(views.APIView):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
def post(self, request):
|
||||||
|
print("request.data====>",request.data)
|
||||||
|
print("request.user====>", request.user)
|
||||||
|
serializer = ChangePasswordSerializer(context=request.user, data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
serializer.save()
|
||||||
|
return custom_response(SUCCESS_CODE['3006'], response_status=status.HTTP_200_OK)
|
||||||
|
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
class ResetPasswordAPIView(views.APIView):
|
class ResetPasswordAPIView(views.APIView):
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
print("request.data====>",request.data)
|
print("request.data====>",request.data)
|
||||||
@ -42,9 +55,7 @@ class ForgotPasswordAPIView(views.APIView):
|
|||||||
return custom_error_response(ERROR_CODE['2004'], response_status=status.HTTP_404_NOT_FOUND)
|
return custom_error_response(ERROR_CODE['2004'], response_status=status.HTTP_404_NOT_FOUND)
|
||||||
verification_code = ''.join([str(random.randrange(9)) for _ in range(6)])
|
verification_code = ''.join([str(random.randrange(9)) for _ in range(6)])
|
||||||
# Send the verification code to the user's email
|
# Send the verification code to the user's email
|
||||||
subject = 'Password Reset Verification Code'
|
from_email = settings.EMAIL_HOST_USER
|
||||||
message = f'Your verification code is: {verification_code}'
|
|
||||||
from_email = 'infozodbank@gmail.com'
|
|
||||||
recipient_list = [email]
|
recipient_list = [email]
|
||||||
send_templated_mail(
|
send_templated_mail(
|
||||||
template_name='email_reset_verification.email',
|
template_name='email_reset_verification.email',
|
||||||
@ -102,7 +113,7 @@ class UserLogin(viewsets.ViewSet):
|
|||||||
serializer = JuniorSerializer(junior_data)
|
serializer = JuniorSerializer(junior_data)
|
||||||
if user.is_superuser:
|
if user.is_superuser:
|
||||||
serializer = SuperUserSerializer(user)
|
serializer = SuperUserSerializer(user)
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
return custom_response(SUCCESS_CODE['3003'], serializer.data, response_status=status.HTTP_200_OK)
|
||||||
else:
|
else:
|
||||||
return custom_error_response(ERROR_CODE["2002"], response_status=status.HTTP_401_UNAUTHORIZED)
|
return custom_error_response(ERROR_CODE["2002"], response_status=status.HTTP_401_UNAUTHORIZED)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@ -55,7 +55,7 @@ SUCCESS_CODE = {
|
|||||||
# Success code for Thank you
|
# Success code for Thank you
|
||||||
"3002": "Thank you for contacting us! Our Consumer Experience Team will reach out to you shortly.",
|
"3002": "Thank you for contacting us! Our Consumer Experience Team will reach out to you shortly.",
|
||||||
# Success code for account activation
|
# Success code for account activation
|
||||||
"3003": "Your account has been activated.",
|
"3003": "Log in successfully",
|
||||||
# Success code for password reset
|
# Success code for password reset
|
||||||
"3004": "Password reset link has been sent to your email address",
|
"3004": "Password reset link has been sent to your email address",
|
||||||
# Success code for link verified
|
# Success code for link verified
|
||||||
|
|||||||
@ -11,7 +11,7 @@ router = routers.SimpleRouter()
|
|||||||
|
|
||||||
"""API End points with router"""
|
"""API End points with router"""
|
||||||
router.register('sign-up', SignupViewset, basename='sign-up')
|
router.register('sign-up', SignupViewset, basename='sign-up')
|
||||||
router.register('update-guardian-profile', UpdateGuardianProfile, basename='update-guardian-profile')
|
router.register('complete-guardian-profile', UpdateGuardianProfile, basename='update-guardian-profile')
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/v1/', include(router.urls)),
|
path('api/v1/', include(router.urls)),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -10,7 +10,7 @@ from rest_framework import routers
|
|||||||
router = routers.SimpleRouter()
|
router = routers.SimpleRouter()
|
||||||
|
|
||||||
"""API End points with router"""
|
"""API End points with router"""
|
||||||
router.register('profile-update', UpdateJuniorProfile, basename='profile-update')
|
router.register('complete-junior-profile', UpdateJuniorProfile, basename='profile-update')
|
||||||
router.register('validate-guardian-code', ValidateGuardianCode, basename='validate-guardian-code')
|
router.register('validate-guardian-code', ValidateGuardianCode, basename='validate-guardian-code')
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/v1/', include(router.urls)),
|
path('api/v1/', include(router.urls)),
|
||||||
|
|||||||
Reference in New Issue
Block a user