Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support change password endpoint #47

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading