chore: change password endpoint (#2842)

This commit is contained in:
Bavisetti Narayan 2023-11-23 13:44:50 +05:30 committed by sriram veeraghanta
parent 7169463ee7
commit f9590929dc
2 changed files with 14 additions and 12 deletions

View File

@ -155,6 +155,16 @@ class ChangePasswordSerializer(serializers.Serializer):
"""
old_password = serializers.CharField(required=True)
new_password = serializers.CharField(required=True)
confirm_password = serializers.CharField(required=True)
def validate(self, data):
if data.get("old_password") == data.get("new_password"):
raise serializers.ValidationError("New password cannot be same as old password.")
if data.get("new_password") != data.get("confirm_password"):
raise serializers.ValidationError("confirm password should be same as the new password.")
return data
class ResetPasswordSerializer(serializers.Serializer):

View File

@ -131,21 +131,13 @@ class ChangePasswordEndpoint(BaseAPIView):
user = User.objects.get(pk=request.user.id)
if serializer.is_valid():
# Check old password
if not user.object.check_password(serializer.data.get("old_password")):
if not user.check_password(serializer.data.get("old_password")):
return Response(
{"old_password": ["Wrong password."]},
status=status.HTTP_400_BAD_REQUEST,
)
# set_password also hashes the password that the user will get
self.object.set_password(serializer.data.get("new_password"))
self.object.save()
response = {
"status": "success",
"code": status.HTTP_200_OK,
"message": "Password updated successfully",
}
return Response(response)
user.set_password(serializer.data.get("new_password"))
user.save()
return Response({"message": "Password updated successfully"}, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)