Skip to content

Commit

Permalink
Merge pull request #47 from codescalersinternships/development_1.1_ch…
Browse files Browse the repository at this point in the history
…ange_password

Support change password endpoint
  • Loading branch information
Mahmoud-Emad authored Aug 11, 2024
2 parents c7bb0f9 + 8e8a297 commit 52d1d33
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions server/test_tracker/routs/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path

from test_tracker.views.auth import (
ChangePasswordView,
DecodeAndVerifySignatureAPIView,
GetUserAPIView,
LoginByTokenAPIView,
Expand All @@ -21,4 +22,5 @@
path("settings/", UpdateUserSettingsAPIView.as_view()),
path("github/access_token/", GithubAccessTokenAPIView.as_view()),
path("github/user/", GithubUserDataAPIView.as_view()),
path("change-password/", ChangePasswordView.as_view()),
]
6 changes: 5 additions & 1 deletion server/test_tracker/serializers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,8 @@ class GitHubRequestToGetAccessTokenSerializers(Serializer):
code = CharField()

class GitHubUserDataSerializers(Serializer):
access_token = CharField()
access_token = CharField()

class ChangePasswordSerializer(Serializer):
old_password = CharField()
new_password = CharField()
39 changes: 35 additions & 4 deletions server/test_tracker/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from test_tracker.api.response import CustomResponse
from test_tracker.models.users import User
from test_tracker.serializers.auth import (
ChangePasswordSerializer,
GitHubRequestToGetAccessTokenSerializers,
GitHubUserDataSerializers,
MyTokenObtainPairSerializer,
Expand All @@ -26,6 +27,8 @@
from components import config

from test_tracker.utils.generate_password import generate_password
from django.contrib.auth.hashers import check_password



class RegisterAPIView(GenericAPIView):
Expand Down Expand Up @@ -109,6 +112,34 @@ def get(self, request: Request, email: str) -> Response:
message="User not found",
)

class ChangePasswordView(GenericAPIView):
serializer_class = ChangePasswordSerializer
permission_classes = [
UserIsAuthenticated,
]

def put(self, request: Request) -> Response:
"""Class change password to change user password."""
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
user_password: str = request.user.password

new_password = make_password(serializer.validated_data.get("new_password"))
checked_password: bool = check_password(
serializer.validated_data.get("old_password"), user_password
)

if checked_password:
request.user.password = new_password
request.user.save()
return CustomResponse.success(message="Success updated password")
return CustomResponse.unauthorized(
message="Incorrect password. Please ensure that the password provided is accurate."
)
return CustomResponse.bad_request(
message="Please make sure that you entered a valid data.",
error=serializer.errors,
)

class UpdateUserSettingsAPIView(GenericAPIView):
"""This class to update profile info"""
Expand All @@ -120,10 +151,10 @@ def put(self, request: Request) -> Response:
"""Update user settings"""
user = get_user_by_id(request.user.id)
serializer = self.get_serializer(user, data=request.data)
if not request.data.get("password"):
request.data["password"] = user.password
else:
request.data["password"] = make_password(request.data["password"])
# if not request.data.get("password"):
# request.data["password"] = user.password
# else:
# request.data["password"] = make_password(request.data["password"])
if serializer.is_valid():
serializer.save()
return CustomResponse.success(
Expand Down

0 comments on commit 52d1d33

Please sign in to comment.