Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /fastapi-users_fastapi-users /fastapi_users /router /reset.py
| from fastapi import APIRouter, Body, Depends, HTTPException, Request, status | |
| from pydantic import EmailStr | |
| from fastapi_users import exceptions, models | |
| from fastapi_users.manager import BaseUserManager, UserManagerDependency | |
| from fastapi_users.openapi import OpenAPIResponseType | |
| from fastapi_users.router.common import ErrorCode, ErrorModel | |
| RESET_PASSWORD_RESPONSES: OpenAPIResponseType = { | |
| status.HTTP_400_BAD_REQUEST: { | |
| "model": ErrorModel, | |
| "content": { | |
| "application/json": { | |
| "examples": { | |
| ErrorCode.RESET_PASSWORD_BAD_TOKEN: { | |
| "summary": "Bad or expired token.", | |
| "value": {"detail": ErrorCode.RESET_PASSWORD_BAD_TOKEN}, | |
| }, | |
| ErrorCode.RESET_PASSWORD_INVALID_PASSWORD: { | |
| "summary": "Password validation failed.", | |
| "value": { | |
| "detail": { | |
| "code": ErrorCode.RESET_PASSWORD_INVALID_PASSWORD, | |
| "reason": "Password should be at least 3 characters", | |
| } | |
| }, | |
| }, | |
| } | |
| } | |
| }, | |
| }, | |
| } | |
| def get_reset_password_router( | |
| get_user_manager: UserManagerDependency[models.UP, models.ID], | |
| ) -> APIRouter: | |
| """Generate a router with the reset password routes.""" | |
| router = APIRouter() | |
| async def forgot_password( | |
| request: Request, | |
| email: EmailStr = Body(..., embed=True), | |
| user_manager: BaseUserManager[models.UP, models.ID] = Depends(get_user_manager), | |
| ): | |
| try: | |
| user = await user_manager.get_by_email(email) | |
| except exceptions.UserNotExists: | |
| return None | |
| try: | |
| await user_manager.forgot_password(user, request) | |
| except exceptions.UserInactive: | |
| pass | |
| return None | |
| async def reset_password( | |
| request: Request, | |
| token: str = Body(...), | |
| password: str = Body(...), | |
| user_manager: BaseUserManager[models.UP, models.ID] = Depends(get_user_manager), | |
| ): | |
| try: | |
| await user_manager.reset_password(token, password, request) | |
| except ( | |
| exceptions.InvalidResetPasswordToken, | |
| exceptions.UserNotExists, | |
| exceptions.UserInactive, | |
| ): | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail=ErrorCode.RESET_PASSWORD_BAD_TOKEN, | |
| ) | |
| except exceptions.InvalidPasswordException as e: | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail={ | |
| "code": ErrorCode.RESET_PASSWORD_INVALID_PASSWORD, | |
| "reason": e.reason, | |
| }, | |
| ) | |
| return router | |
Xet Storage Details
- Size:
- 3.19 kB
- Xet hash:
- 0cb877ac3900d40afd9d55b7c4aa96e90f610c84e4750ead0c7b509e9c50e9a9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.