From 9bd48ffd9361f386b49820c0b1b4b292d93caa7b Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Fri, 29 Dec 2023 00:12:30 -0800 Subject: [PATCH] feat: change password support --- backend/apps/web/models/auths.py | 24 ++++++- backend/apps/web/routers/auths.py | 19 ++++++ src/lib/components/chat/SettingsModal.svelte | 66 ++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/backend/apps/web/models/auths.py b/backend/apps/web/models/auths.py index 1bd4b4cb7..ce0873164 100644 --- a/backend/apps/web/models/auths.py +++ b/backend/apps/web/models/auths.py @@ -64,6 +64,11 @@ class SigninForm(BaseModel): password: str +class UpdatePasswordForm(BaseModel): + password: str + new_password: str + + class SignupForm(BaseModel): name: str email: str @@ -109,7 +114,24 @@ class AuthsTable: except: return None - def delete_auth_by_id(self, id: str) -> Optional[UserModel]: + def update_user_password_by_id( + self, id: str, password: str, new_password: str + ) -> bool: + try: + auth = Auth.get(Auth.id == id, Auth.active == True) + if auth: + if verify_password(password, auth.password): + query = Auth.update(password=new_password).where(Auth.id == id) + result = query.execute() + print(result) + return True + else: + return False + return True + except: + return False + + def delete_auth_by_id(self, id: str) -> bool: try: # Delete User result = Users.delete_user_by_id(id) diff --git a/backend/apps/web/routers/auths.py b/backend/apps/web/routers/auths.py index 27d6a3b6a..31b41b6d2 100644 --- a/backend/apps/web/routers/auths.py +++ b/backend/apps/web/routers/auths.py @@ -11,6 +11,7 @@ import uuid from apps.web.models.auths import ( SigninForm, SignupForm, + UpdatePasswordForm, UserResponse, SigninResponse, Auths, @@ -53,6 +54,24 @@ async def get_session_user(cred=Depends(bearer_scheme)): ) +############################ +# Update Password +############################ + + +@router.post("/update/password", response_model=bool) +async def update_password(form_data: UpdatePasswordForm, cred=Depends(bearer_scheme)): + token = cred.credentials + user = Users.get_user_by_token(token) + + if user: + hashed = get_password_hash(form_data.new_password) + return Auths.update_user_password_by_id(user.id, form_data.password, hashed) + + else: + raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED) + + ############################ # SignIn ############################ diff --git a/src/lib/components/chat/SettingsModal.svelte b/src/lib/components/chat/SettingsModal.svelte index 05dcefd18..e5332296f 100644 --- a/src/lib/components/chat/SettingsModal.svelte +++ b/src/lib/components/chat/SettingsModal.svelte @@ -118,6 +118,11 @@ let authType = 'Basic'; let authContent = ''; + // Account + let currentPassword = ''; + let newPassword = ''; + let newPasswordConfirm = ''; + // About let ollamaVersion = ''; @@ -1843,6 +1848,67 @@ + {:else if selectedTab === 'account'} +
{ + console.log('change save'); + }} + > +
Change Password
+ +
+
+
Current Password
+ +
+ +
+
+ +
+
New Password
+ +
+ +
+
+ +
+
Confirm Password
+ +
+ +
+
+
+ +
+ +
+
{:else if selectedTab === 'about'}