Updated to expose auth_app instead of app_route
This commit is contained in:
22
src/main.py
Normal file
22
src/main.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
|
||||
from myauth import create_auth_app_for_sqlite
|
||||
|
||||
# 1. Initialize FastAPI app
|
||||
app = FastAPI()
|
||||
|
||||
# 2. Configure repositories for MongoDB
|
||||
auth_app = create_auth_app_for_sqlite(db_path="./UserDB", jwt_secret="THIS_NEEDS_TO_BE_CHANGED")
|
||||
|
||||
# 3. Include the authentication routes
|
||||
app.mount("/auth", auth_app)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"message": "Application running with MyAuth (SQLite)"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
@@ -1,6 +1,9 @@
|
||||
from .factory import create_sqlite_auth_service, create_app_router_for_sqlite
|
||||
from .factory import create_sqlite_auth_service, create_auth_app_for_sqlite, create_auth_app_for_mongodb, \
|
||||
create_auth_app_for_postgresql
|
||||
|
||||
__all__ = [
|
||||
'create_sqlite_auth_service',
|
||||
'create_app_router_for_sqlite',
|
||||
'create_auth_app_for_sqlite',
|
||||
'create_auth_app_for_mongodb',
|
||||
'create_auth_app_for_postgresql'
|
||||
]
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
from .routes import create_auth_router
|
||||
from .routes import create_auth_app
|
||||
|
||||
__all__ = ["create_auth_router"]
|
||||
__all__ = ["create_auth_app"]
|
||||
@@ -7,7 +7,7 @@ operations. Routes are organized in an APIRouter with /auth prefix.
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import Depends, HTTPException, status, FastAPI
|
||||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
||||
|
||||
from ..core.auth import AuthService
|
||||
@@ -24,7 +24,7 @@ from ..models.user import UserCreate, UserResponse
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
||||
|
||||
|
||||
def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
def create_auth_app(auth_service: AuthService) -> FastAPI:
|
||||
"""
|
||||
Create and configure the authentication router.
|
||||
|
||||
@@ -40,13 +40,13 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
|
||||
Example:
|
||||
>>> from fastapi import FastAPI
|
||||
>>> from auth_module.api.routes import create_auth_router
|
||||
>>> from myauth.api.routes import create_auth_app
|
||||
>>>
|
||||
>>> app = FastAPI()
|
||||
>>> auth_router = create_auth_router(auth_service)
|
||||
>>> app.include_router(auth_router)
|
||||
>>> auth_api = create_auth_app(auth_service)
|
||||
>>> app.mount(auth_api)
|
||||
"""
|
||||
router = APIRouter(prefix="/auth", tags=["authentication"])
|
||||
auth_app = FastAPI(prefix="/auth", tags=["authentication"])
|
||||
|
||||
def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> UserResponse:
|
||||
"""
|
||||
@@ -65,7 +65,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
HTTPException: 401 if token is invalid or expired.
|
||||
|
||||
Example:
|
||||
>>> @app.get("/protected")
|
||||
>>> @auth_app.get("/protected")
|
||||
>>> def protected_route(user: UserResponse = Depends(get_current_user)):
|
||||
>>> return {"user_id": user.id}
|
||||
"""
|
||||
@@ -86,7 +86,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
detail=e.message
|
||||
)
|
||||
|
||||
@router.post(
|
||||
@auth_app.post(
|
||||
"/register",
|
||||
response_model=UserResponse,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
@@ -128,7 +128,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
detail=e.message
|
||||
)
|
||||
|
||||
@router.post(
|
||||
@auth_app.post(
|
||||
"/login",
|
||||
response_model=AccessTokenResponse,
|
||||
summary="Login with email and password",
|
||||
@@ -163,7 +163,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
detail=e.message
|
||||
)
|
||||
|
||||
@router.post(
|
||||
@auth_app.post(
|
||||
"/logout",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
summary="Logout user",
|
||||
@@ -185,7 +185,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
auth_service.logout(request.refresh_token)
|
||||
return None
|
||||
|
||||
@router.post(
|
||||
@auth_app.post(
|
||||
"/refresh",
|
||||
response_model=AccessTokenResponse,
|
||||
summary="Refresh access token",
|
||||
@@ -217,7 +217,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
detail=e.message
|
||||
)
|
||||
|
||||
@router.post(
|
||||
@auth_app.post(
|
||||
"/password-reset-request",
|
||||
status_code=status.HTTP_200_OK,
|
||||
summary="Request password reset",
|
||||
@@ -252,7 +252,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
detail=e.message
|
||||
)
|
||||
|
||||
@router.post(
|
||||
@auth_app.post(
|
||||
"/password-reset",
|
||||
status_code=status.HTTP_200_OK,
|
||||
summary="Reset password with token",
|
||||
@@ -284,7 +284,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
detail=e.message
|
||||
)
|
||||
|
||||
@router.post(
|
||||
@auth_app.post(
|
||||
"/verify-email-request",
|
||||
status_code=status.HTTP_200_OK,
|
||||
summary="Request email verification",
|
||||
@@ -320,7 +320,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
detail=e.message
|
||||
)
|
||||
|
||||
@router.get(
|
||||
@auth_app.get(
|
||||
"/verify-email",
|
||||
status_code=status.HTTP_200_OK,
|
||||
summary="Verify email with token",
|
||||
@@ -352,7 +352,7 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
detail=e.message
|
||||
)
|
||||
|
||||
@router.get(
|
||||
@auth_app.get(
|
||||
"/me",
|
||||
response_model=UserResponse,
|
||||
summary="Get current user",
|
||||
@@ -376,4 +376,4 @@ def create_auth_router(auth_service: AuthService) -> APIRouter:
|
||||
"""
|
||||
return current_user
|
||||
|
||||
return router
|
||||
return auth_app
|
||||
|
||||
@@ -57,9 +57,9 @@ def create_sqlite_auth_service(db_path: str,
|
||||
return auth_service
|
||||
|
||||
|
||||
def create_app_router_for_sqlite(db_path: str,
|
||||
jwt_secret: str,
|
||||
email_service: Optional[EmailService] = None):
|
||||
def create_auth_app_for_sqlite(db_path: str,
|
||||
jwt_secret: str,
|
||||
email_service: Optional[EmailService] = None):
|
||||
"""
|
||||
Creates an application router designed to use with an SQLite database backend.
|
||||
This function initializes an authentication service using the provided database
|
||||
@@ -74,5 +74,17 @@ def create_app_router_for_sqlite(db_path: str,
|
||||
:return: An application router configured for handling authentication API routes.
|
||||
"""
|
||||
auth_service = create_sqlite_auth_service(db_path, jwt_secret, email_service)
|
||||
from .api.routes import create_auth_router
|
||||
return create_auth_router(auth_service)
|
||||
from .api.routes import create_auth_app
|
||||
return create_auth_app(auth_service)
|
||||
|
||||
|
||||
def create_auth_app_for_mongodb(mongodb_url="mongodb://localhost:27017",
|
||||
jwt_secret="THIS_NEEDS_TO_BE_CHANGED"):
|
||||
raise NotImplementedError("MongoDB support is not yet implemented.")
|
||||
|
||||
|
||||
def create_auth_app_for_postgresql(postgresql_url="mongodb://localhost:27017",
|
||||
username="admin",
|
||||
password="password",
|
||||
jwt_secret="THIS_NEEDS_TO_BE_CHANGED"):
|
||||
raise NotImplementedError("PostgreSQL support is not yet implemented.")
|
||||
|
||||
Reference in New Issue
Block a user