mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-11-03 22:22:06 +08:00
* Added login endpoint docs
* make format
* Changed login route into multiple examples.
* Added reset password router docs
* Updated /{id} routes for user
* Updated /me routes
* Fixed user already exists response description
* Updated the /register route
* Updated verify routes
* Updated oauth2 endpoints.
* Applied `make format`
* Renamed Authentication methods for getting their openapi schemas
- `get_login_responses_success` -> `get_openapi_login_responses_success`
- `get_logout_responses_success` -> `get_openapi_logout_responses_success`
* Fixed flake8 errors
* Not using `Final` to keep python37 compatibility
Co-authored-by: François Voron <fvoron@gmail.com>
80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
from typing import Type
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
|
|
|
from fastapi_users import models
|
|
from fastapi_users.manager import (
|
|
BaseUserManager,
|
|
InvalidPasswordException,
|
|
UserAlreadyExists,
|
|
UserManagerDependency,
|
|
)
|
|
from fastapi_users.router.common import ErrorCode, ErrorModel
|
|
|
|
|
|
def get_register_router(
|
|
get_user_manager: UserManagerDependency[models.UC, models.UD],
|
|
user_model: Type[models.U],
|
|
user_create_model: Type[models.UC],
|
|
) -> APIRouter:
|
|
"""Generate a router with the register route."""
|
|
router = APIRouter()
|
|
|
|
@router.post(
|
|
"/register",
|
|
response_model=user_model,
|
|
status_code=status.HTTP_201_CREATED,
|
|
name="register:register",
|
|
responses={
|
|
status.HTTP_400_BAD_REQUEST: {
|
|
"model": ErrorModel,
|
|
"content": {
|
|
"application/json": {
|
|
"examples": {
|
|
ErrorCode.REGISTER_USER_ALREADY_EXISTS: {
|
|
"summary": "A user with this email already exists.",
|
|
"value": {
|
|
"detail": ErrorCode.REGISTER_USER_ALREADY_EXISTS
|
|
},
|
|
},
|
|
ErrorCode.REGISTER_INVALID_PASSWORD: {
|
|
"summary": "Password validation failed.",
|
|
"value": {
|
|
"detail": {
|
|
"code": ErrorCode.REGISTER_INVALID_PASSWORD,
|
|
"reason": "Password should be"
|
|
"at least 3 characters",
|
|
}
|
|
},
|
|
},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
},
|
|
)
|
|
async def register(
|
|
request: Request,
|
|
user: user_create_model, # type: ignore
|
|
user_manager: BaseUserManager[models.UC, models.UD] = Depends(get_user_manager),
|
|
):
|
|
try:
|
|
created_user = await user_manager.create(user, safe=True, request=request)
|
|
except UserAlreadyExists:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=ErrorCode.REGISTER_USER_ALREADY_EXISTS,
|
|
)
|
|
except InvalidPasswordException as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail={
|
|
"code": ErrorCode.REGISTER_INVALID_PASSWORD,
|
|
"reason": e.reason,
|
|
},
|
|
)
|
|
|
|
return created_user
|
|
|
|
return router
|