Commit c625d996 authored by Tomas Vaagen Gule's avatar Tomas Vaagen Gule
Browse files

#9 Add possibilty to change password

parent a7ed0a92
Pipeline #162271 passed with stages
in 1 minute and 51 seconds
......@@ -55,6 +55,33 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
return user_obj
class ChangePasswordSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=True)
password2 = serializers.CharField(write_only=True, required=True)
old_password = serializers.CharField(write_only=True, required=True)
class Meta:
model = get_user_model()
fields = ('old_password', 'password', 'password2')
def validate(self, attrs):
if attrs['password'] != attrs['password2']:
raise serializers.ValidationError({"password": "Password fields didn't match."})
return attrs
def validate_old_password(self, value):
user = self.context['request'].user
if not user.check_password(value):
raise serializers.ValidationError({"old_password": "Old password is not correct"})
return value
def update(self, instance, validated_data):
instance.set_password(validated_data['password'])
instance.save()
return instance
class UserGetSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
......
from django.urls import path, include
from .views import ChangePasswordView
from users import views
from rest_framework.urlpatterns import format_suffix_patterns
......@@ -16,4 +17,5 @@ urlpatterns = [
views.AthleteFileDetail.as_view(),
name="athletefile-detail",
),
path('api/change_password/<int:pk>/', ChangePasswordView.as_view(), name='auth_change_password'),
]
import django
from .serializers import ChangePasswordSerializer
from rest_framework import mixins, generics
from workouts.mixins import CreateListModelMixin
from rest_framework import permissions
......@@ -81,6 +82,11 @@ class UserDetail(
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
class ChangePasswordView(generics.UpdateAPIView):
queryset = get_user_model().objects.all()
permission_classes = (IsAuthenticated,)
serializer_class = ChangePasswordSerializer
class OfferList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
......
......@@ -117,6 +117,47 @@
</div>
<!-- form-group// -->
</form>
<form id="form-change-password">
<div class="input-group mb-2">
<span class="input-group-text"> <i class="fas fa-lock"></i> </span>
<input
class="form-control"
name="old_password"
placeholder="Old password"
type="password"
required
/>
</div>
<div class="input-group mb-2">
<span class="input-group-text"> <i class="fas fa-lock"></i> </span>
<input
class="form-control"
name="password"
placeholder="New password"
type="password"
required
/>
</div>
<div class="input-group mb-2">
<span class="input-group-text"> <i class="fas fa-lock"></i> </span>
<input
class="form-control"
name="password2"
placeholder="Repeat password"
type="password"
required
/>
</div>
<div class="d-grid">
<input
type="button"
class="btn btn-primary"
id="btn-change-password"
value=" Change password "
/>
</div>
<!-- form-group// -->
</form>
</div>
<script src="scripts/defaults.js"></script>
......
......@@ -30,7 +30,7 @@ function handleChangesButton() {
setReadOnly(true, "#form-register-user");
document.querySelector("#username").readOnly = true;
console.log("Test");
createNewUser();
updateUser();
}
function handleEditButton() {
......@@ -46,8 +46,7 @@ function handleEditButton() {
document.querySelector("#username").readOnly = true;
}
async function createNewUser() {
console.log("Test");
async function updateUser() {
let form = document.querySelector("#form-register-user");
let formData = new FormData(form);
......@@ -66,6 +65,27 @@ async function createNewUser() {
window.location.replace("mypage.html");
}
async function changePassword() {
let form = document.querySelector("#form-change-password");
let formData = new FormData(form);
let body = {
old_password: formData.get("old_password"),
password: formData.get("password"),
password2: formData.get("password2"),
};
user = await getCurrentUser();
response = await sendRequest(
"PUT",
`${HOST}/api/change_password/${user.id}/`,
body
);
if (response.ok) {
} else {
}
window.location.replace("mypage.html");
}
window.addEventListener("DOMContentLoaded", async () => {
setUser();
......@@ -73,4 +93,6 @@ window.addEventListener("DOMContentLoaded", async () => {
btnEditAccount.addEventListener("click", handleEditButton);
btnConfirmChanges = document.querySelector("#btn-confirm-changes");
btnConfirmChanges.addEventListener("click", handleChangesButton);
btnChangePassword = document.querySelector("#btn-change-password");
btnChangePassword.addEventListener("click", changePassword);
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment