mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: add password change page & feature
This commit is contained in:
parent
1157b77835
commit
be0c2ef262
4 changed files with 103 additions and 0 deletions
|
|
@ -0,0 +1,56 @@
|
||||||
|
"use client";
|
||||||
|
import { AuthContext } from '@components/Security/AuthProvider';
|
||||||
|
import React, { useEffect } from 'react'
|
||||||
|
import { Formik, Form, Field, ErrorMessage } from 'formik';
|
||||||
|
import { updateProfile } from '@services/settings/profile';
|
||||||
|
import { updatePassword } from '@services/settings/password';
|
||||||
|
|
||||||
|
function SettingsProfilePasswordsPage() {
|
||||||
|
|
||||||
|
const auth: any = React.useContext(AuthContext);
|
||||||
|
|
||||||
|
const updatePasswordUI = async (values: any) => {
|
||||||
|
let user_id = auth.userInfo.user_object.user_id;
|
||||||
|
console.log(values);
|
||||||
|
await updatePassword(user_id, values)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
|
||||||
|
{auth.isAuthenticated && (
|
||||||
|
<div>
|
||||||
|
<h1>Account Password</h1>
|
||||||
|
<br /><br />
|
||||||
|
|
||||||
|
<Formik
|
||||||
|
initialValues={{ old_password: '', new_password: '' }}
|
||||||
|
onSubmit={(values, { setSubmitting }) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
alert(JSON.stringify(values, null, 2));
|
||||||
|
setSubmitting(false);
|
||||||
|
updatePasswordUI(values)
|
||||||
|
}, 400);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{({ isSubmitting }) => (
|
||||||
|
<Form>
|
||||||
|
Old Password <Field type="password" name="old_password" /><br />
|
||||||
|
New password <Field type="password" name="new_password" /><br />
|
||||||
|
<button type="submit" disabled={isSubmitting}>
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
|
</Form>
|
||||||
|
)}
|
||||||
|
</Formik>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default SettingsProfilePasswordsPage
|
||||||
15
front/services/settings/password.ts
Normal file
15
front/services/settings/password.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { getAPIUrl } from "@services/config";
|
||||||
|
import { RequestBody } from "@services/utils/requests";
|
||||||
|
|
||||||
|
/*
|
||||||
|
This file includes only POST, PUT, DELETE requests
|
||||||
|
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||||
|
*/
|
||||||
|
|
||||||
|
export async function updatePassword(user_id : string, data: any) {
|
||||||
|
const result: any = await fetch(`${getAPIUrl()}users/password/user_id/` + user_id, RequestBody("PUT", data))
|
||||||
|
.then((result) => result.json())
|
||||||
|
.catch((error) => console.log("error", error));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
@ -64,3 +64,10 @@ async def api_update_user(request: Request, user_object: User, user_id: str):
|
||||||
Update user by ID
|
Update user by ID
|
||||||
"""
|
"""
|
||||||
return await update_user(request, user_id, user_object)
|
return await update_user(request, user_id, user_object)
|
||||||
|
|
||||||
|
@router.put("/password/user_id/{user_id}")
|
||||||
|
async def api_update_user_password(request: Request, user_id: str , passwordChangeForm : PasswordChangeForm):
|
||||||
|
"""
|
||||||
|
Update user password by ID
|
||||||
|
"""
|
||||||
|
return await update_user_password(request, user_id, passwordChangeForm)
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,10 @@ class PublicUser(User):
|
||||||
creationDate: str
|
creationDate: str
|
||||||
updateDate: str
|
updateDate: str
|
||||||
|
|
||||||
|
class PasswordChangeForm(BaseModel):
|
||||||
|
old_password: str
|
||||||
|
new_password: str
|
||||||
|
|
||||||
|
|
||||||
class UserInDB(UserWithPassword):
|
class UserInDB(UserWithPassword):
|
||||||
user_id: str
|
user_id: str
|
||||||
|
|
@ -157,6 +161,27 @@ async def update_user(request: Request, user_id: str, user_object: User):
|
||||||
return User(**user_object.dict())
|
return User(**user_object.dict())
|
||||||
|
|
||||||
|
|
||||||
|
async def update_user_password(request: Request, user_id: str, password_change_form: PasswordChangeForm):
|
||||||
|
users = request.app.db["users"]
|
||||||
|
|
||||||
|
isUserExists = await users.find_one({"user_id": user_id})
|
||||||
|
|
||||||
|
if not isUserExists:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_409_CONFLICT, detail="User does not exist")
|
||||||
|
|
||||||
|
if not await security_verify_password(password_change_form.old_password, isUserExists["password"]):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED, detail="Wrong password")
|
||||||
|
|
||||||
|
new_password = await security_hash_password(password_change_form.new_password)
|
||||||
|
|
||||||
|
updated_user = {"$set": {"password": new_password}}
|
||||||
|
users.update_one({"user_id": user_id}, updated_user)
|
||||||
|
|
||||||
|
return {"detail": "Password updated"}
|
||||||
|
|
||||||
|
|
||||||
async def delete_user(request: Request, user_id: str):
|
async def delete_user(request: Request, user_id: str):
|
||||||
users = request.app.db["users"]
|
users = request.app.db["users"]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue