ft_transcendence/accounts/views/change_password.py

25 lines
1.0 KiB
Python
Raw Normal View History

2023-11-11 13:50:14 -05:00
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, status
from django.http import HttpRequest
from django.contrib.auth import login
from rest_framework.authentication import SessionAuthentication
from django.contrib.auth.models import User
2023-11-11 13:50:14 -05:00
from ..serializers.change_password import ChangePasswordSerializer
class ChangePasswordView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
2023-10-25 10:10:23 -04:00
def post(self, request: HttpRequest):
2023-11-13 07:22:10 -05:00
data = request.data
2023-10-25 10:10:23 -04:00
2023-11-11 13:50:14 -05:00
serializer = ChangePasswordSerializer(data=data)
if serializer.is_valid(raise_exception=True):
user: User = request.user
if (user.check_password(data['current_password']) == 0):
return Response({'current_password': "The password is not right."}, status=status.HTTP_200_OK)
user.set_password(data["new_password"])
return Response('password changed', status=status.HTTP_200_OK)