Код: Выделить всё
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
sub_app1 = FastAPI()
sub_app1.include_router(auth.router)
Код: Выделить всё
@router.post("/complete", status_code=status.HTTP_201_CREATED)
async def complete_registration_create_user(db: db_dependency, create_user_request: CreateUserRequest):
"""Compeletes the registration when user submits password."""
hashed_password = bcrypt_context.hash(create_user_request.password)
create_user_model = Users(
email=create_user_request.email,
hashed_password=hashed_password,
phone=create_user_request.phone,
)
# Note: In a organisation all staff members should be approved by Owner.
try:
db.add(create_user_model)
await db.commit()
except IntegrityError as er:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"User {create_user_request.email} already exists. Choose a different email or try resetting the password."
)
await db.refresh(create_user_model)
return {"user": create_user_request.email}
@router.post("/token", response_model=Token)
async def user_login_for_access_token(form_data: Annotated[OAuth2EmailRequestForm, Depends()],
db: db_dependency):
"""Login user for an access token"""
user = await authenticate_user(form_data.email, form_data.password, db)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate user."
)
tenant_identifier = form_data.identifier
token = create_access_token(
user.email,
user.id,
tenant_identifier,
timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
)
return {'access_token': token, 'token_type': 'bearer'}
Код: Выделить всё
def custom_openapi():
if sub_app1.openapi_schema:
return sub_app1.openapi_schema
openapi_schema = get_openapi(
title=" Authorisation API",
version="0.0.1",
description="API on this docs deals with all user and company registration.",
routes=sub_app1.routes,
)
sub_app1.openapi_schema = openapi_schema
return sub_app1.openapi_schema
sub_app1.openapi = custom_openapi
Код: Выделить всё
INFO: 127.0.0.1:43756 - "POST /register/token?email=user2%40example.com&identifier=sada&password=string HTTP/1.1" 404 Not Found
INFO: 127.0.0.1:44748 - "POST /register/complete HTTP/1.1" 404 Not Found
Подробнее здесь: https://stackoverflow.com/questions/789 ... n-not-to-w