Skip to content

Commit 2ff0faf

Browse files
Jeny Sadadiagctucker
Jeny Sadadia
authored andcommitted
api.main: add POST /user/update-password endpoint
Implement an endpoint to update a user password. Signed-off-by: Jeny Sadadia <[email protected]>
1 parent 310f3d6 commit 2ff0faf

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

api/main.py

+21
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
HTTPException,
1818
status,
1919
Request,
20+
Form
2021
)
2122
from fastapi.responses import JSONResponse, PlainTextResponse
23+
from fastapi.security import OAuth2PasswordRequestForm
2224
from fastapi_pagination import add_pagination
2325
from fastapi_versioning import VersionedFastAPI
2426
from bson import ObjectId, errors
@@ -327,6 +329,25 @@ async def get_users(request: Request):
327329
return paginated_resp
328330

329331

332+
@app.post("/user/update-password", response_model=UserRead, tags=["user"])
333+
async def update_password(request: Request,
334+
credentials: OAuth2PasswordRequestForm = Depends(),
335+
new_password: str = Form(None)):
336+
"""Update user password"""
337+
user = await UserManager(BeanieUserDatabase(User)).authenticate(
338+
credentials)
339+
if user is None or not user.is_active:
340+
raise HTTPException(
341+
status_code=status.HTTP_400_BAD_REQUEST,
342+
detail="LOGIN_BAD_CREDENTIALS",
343+
)
344+
user_update = UserUpdate(password=new_password)
345+
user_from_username = await db.find_one(User, username=credentials.username)
346+
await users_router.routes[3].endpoint(
347+
user_update, request, user_from_username,
348+
UserManager(BeanieUserDatabase(User)))
349+
350+
330351
# -----------------------------------------------------------------------------
331352
# User groups
332353

0 commit comments

Comments
 (0)