From db091176da3be36e85c36532509434b667958563 Mon Sep 17 00:00:00 2001 From: Sam Liu Date: Tue, 11 Jun 2024 12:55:46 +0000 Subject: [PATCH] [FWA-3] fix cannot hash password --- backend/repos/repository_users.py | 3 ++- backend/schemas/user/user.py | 4 ++++ frontend/src/pages/Profile/Profile.jsx | 11 +++++------ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/backend/repos/repository_users.py b/backend/repos/repository_users.py index 8d6e322..d2d15b0 100644 --- a/backend/repos/repository_users.py +++ b/backend/repos/repository_users.py @@ -40,7 +40,8 @@ class RepositoryUsers: if not db_user: return None try: - self.user.query.where(User.id == user_id).update(user.dict(exclude_unset=True)) + user.update_password() + self.user.query.where(User.id == user_id).update(user.dict(exclude_unset=True, exclude_none=True)) db.commit() except Exception: db.rollback() diff --git a/backend/schemas/user/user.py b/backend/schemas/user/user.py index ffa26d4..5a606e7 100644 --- a/backend/schemas/user/user.py +++ b/backend/schemas/user/user.py @@ -3,6 +3,7 @@ from uuid import UUID from pydantic import ConfigDict from fastapi import Form +from backend.core.security.security import hash_password from backend.schemas.main_model import MainModel class UserBase(MainModel): @@ -21,6 +22,9 @@ class UserProfile(MainModel): is_lock: bool | None = None model_config = ConfigDict(from_attributes=True) + def update_password(self): + self.password = (None if self.password is None else hash_password(self.password)) + class UserSeeds(UserCreate): is_admin: bool is_lock: bool diff --git a/frontend/src/pages/Profile/Profile.jsx b/frontend/src/pages/Profile/Profile.jsx index c021971..48fd998 100644 --- a/frontend/src/pages/Profile/Profile.jsx +++ b/frontend/src/pages/Profile/Profile.jsx @@ -32,12 +32,9 @@ export default function Profile() { const { formData } = formHandler createEffect(() => { - formHandler.fillForm( - { - name: userInfo?.name, - }, - { silentValidation: false }, - ) + formHandler.fillForm({ + name: userInfo?.name, + }) }) const submit = async (event) => { @@ -53,6 +50,8 @@ export default function Profile() { if (resp.status === 200) { setUser(resp.data) + formHandler.setFieldValue('password', '') + formHandler.setFieldValue('confirm-password', '') notify.success({ title: 'Update profile success!', description: 'Your profile has been updated!',