Skip to content

Commit

Permalink
api.main: add POST /user/update-password endpoint
Browse files Browse the repository at this point in the history
Implement an endpoint to update a user password.

Signed-off-by: Jeny Sadadia <jeny.sadadia@collabora.com>
  • Loading branch information
Jeny Sadadia authored and gctucker committed Nov 10, 2023
1 parent 310f3d6 commit 2ff0faf
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
HTTPException,
status,
Request,
Form
)
from fastapi.responses import JSONResponse, PlainTextResponse
from fastapi.security import OAuth2PasswordRequestForm
from fastapi_pagination import add_pagination
from fastapi_versioning import VersionedFastAPI
from bson import ObjectId, errors
Expand Down Expand Up @@ -327,6 +329,25 @@ async def get_users(request: Request):
return paginated_resp


@app.post("/user/update-password", response_model=UserRead, tags=["user"])
async def update_password(request: Request,
credentials: OAuth2PasswordRequestForm = Depends(),
new_password: str = Form(None)):
"""Update user password"""
user = await UserManager(BeanieUserDatabase(User)).authenticate(
credentials)
if user is None or not user.is_active:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="LOGIN_BAD_CREDENTIALS",
)
user_update = UserUpdate(password=new_password)
user_from_username = await db.find_one(User, username=credentials.username)
await users_router.routes[3].endpoint(
user_update, request, user_from_username,
UserManager(BeanieUserDatabase(User)))


# -----------------------------------------------------------------------------
# User groups

Expand Down

0 comments on commit 2ff0faf

Please sign in to comment.