|
17 | 17 | HTTPException,
|
18 | 18 | status,
|
19 | 19 | Request,
|
| 20 | + Form |
20 | 21 | )
|
21 | 22 | from fastapi.responses import JSONResponse, PlainTextResponse
|
| 23 | +from fastapi.security import OAuth2PasswordRequestForm |
22 | 24 | from fastapi_pagination import add_pagination
|
23 | 25 | from fastapi_versioning import VersionedFastAPI
|
24 | 26 | from bson import ObjectId, errors
|
@@ -327,6 +329,25 @@ async def get_users(request: Request):
|
327 | 329 | return paginated_resp
|
328 | 330 |
|
329 | 331 |
|
| 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 | + |
330 | 351 | # -----------------------------------------------------------------------------
|
331 | 352 | # User groups
|
332 | 353 |
|
|
0 commit comments