Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mdfarhankc_fastapi-fullauth /fastapi_fullauth /routers /profile.py
| import logging | |
| from typing import TYPE_CHECKING, cast | |
| from fastapi import APIRouter, Body, Depends, HTTPException | |
| from pydantic import BaseModel | |
| from fastapi_fullauth.dependencies.current_user import CurrentUser, VerifiedUser, get_fullauth | |
| from fastapi_fullauth.exceptions import ( | |
| AuthenticationError, | |
| InvalidPasswordError, | |
| NoValidFieldsError, | |
| UnknownFieldsError, | |
| ) | |
| from fastapi_fullauth.flows.change_password import change_password | |
| from fastapi_fullauth.flows.profile import validate_profile_updates | |
| from fastapi_fullauth.routers._schemas import ( | |
| ChangePasswordRequest, | |
| MessageResponse, | |
| build_profile_update_model, | |
| ) | |
| from fastapi_fullauth.types import UserSchema, UserSchemaType | |
| logger = logging.getLogger("fastapi_fullauth.routers") | |
| if TYPE_CHECKING: | |
| from fastapi_fullauth.fullauth import FullAuth | |
| def create_profile_router( | |
| user_schema: type[UserSchemaType] = UserSchema, # type: ignore[assignment] | |
| message_response_schema: type[MessageResponse] = MessageResponse, | |
| ) -> APIRouter: | |
| router = APIRouter() | |
| ProfileUpdate = build_profile_update_model(user_schema) # noqa: N806 | |
| async def me_route( | |
| user: CurrentUser, | |
| ) -> UserSchema: | |
| return user | |
| async def verified_me_route( | |
| user: VerifiedUser, | |
| ) -> UserSchema: | |
| return user | |
| async def update_me_route( | |
| user: CurrentUser, | |
| fullauth: "FullAuth" = Depends(get_fullauth), | |
| data: ProfileUpdate = Body(...), # type: ignore[valid-type] | |
| ) -> UserSchema: | |
| raw = cast("BaseModel", data).model_dump(exclude_unset=True) | |
| try: | |
| updates = validate_profile_updates(raw, user_schema) | |
| except NoValidFieldsError: | |
| raise HTTPException(status_code=400, detail="No valid fields to update") | |
| except UnknownFieldsError as e: | |
| raise HTTPException(status_code=422, detail=str(e)) | |
| return await fullauth.adapter.update_user(user.id, updates) | |
| async def delete_me_route( | |
| user: CurrentUser, | |
| fullauth: "FullAuth" = Depends(get_fullauth), | |
| ) -> None: | |
| await fullauth.adapter.revoke_all_user_refresh_tokens(user.id) | |
| await fullauth.adapter.delete_user(user.id) | |
| logger.warning("Account deleted: user_id=%s, email=%s", user.id, user.email) | |
| async def change_password_route( | |
| data: ChangePasswordRequest, | |
| user: CurrentUser, | |
| fullauth: "FullAuth" = Depends(get_fullauth), | |
| ) -> MessageResponse: | |
| try: | |
| await change_password( | |
| adapter=fullauth.adapter, | |
| user_id=user.id, | |
| new_password=data.new_password, | |
| current_password=data.current_password, | |
| hash_algorithm=fullauth.config.PASSWORD_HASH_ALGORITHM, | |
| password_validator=fullauth.password_validator, | |
| ) | |
| except AuthenticationError: | |
| raise HTTPException(status_code=400, detail="Current password is incorrect") | |
| except InvalidPasswordError as e: | |
| raise HTTPException(status_code=422, detail=str(e)) | |
| await fullauth.hooks.emit("after_password_change", user=user) | |
| return message_response_schema(detail="Password changed successfully.") | |
| return router | |
Xet Storage Details
- Size:
- 4.2 kB
- Xet hash:
- fe2665105a48cb9a8baa171105b2d5ca578b9905467d0f0822ae793c3213961e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.