From 24887274f0f5226a21aca3d56ad98b7d3b40a0df Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 6 Apr 2024 15:36:48 -0400 Subject: [PATCH] add router tags --- backend/routers/auth.py | 13 ++++++++----- backend/routers/forum.py | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/backend/routers/auth.py b/backend/routers/auth.py index 9d1aed4..29ee23d 100644 --- a/backend/routers/auth.py +++ b/backend/routers/auth.py @@ -11,13 +11,16 @@ from jose import jwt, JWTError from models import User from database import SessionLocal -router = APIRouter() +router = APIRouter( + prefix='/auth', + tags=['auth'] +) SECRET_KEY = '3b004eeae34b43bd05226f210d9bdc2ad99abdd3c52bf32802906085b762ff55' ALGORITHM = 'HS256' bcrypt_context = CryptContext(schemes=['bcrypt'], deprecated='auto') -oauth2_bearer = OAuth2PasswordBearer(tokenUrl='validate') +oauth2_bearer = OAuth2PasswordBearer(tokenUrl='auth/token') def get_db(): @@ -72,7 +75,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_bearer)]): raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials") -@router.post('/auth/create', status_code=status.HTTP_201_CREATED) +@router.post('/user/create', status_code=status.HTTP_201_CREATED) async def create_user(db: db_dependency, data: CreateUser): create_user_model = User( username=data.username, @@ -85,14 +88,14 @@ async def create_user(db: db_dependency, data: CreateUser): db.commit() -@router.post('/auth/token', status_code=status.HTTP_200_OK, response_model=Token) +@router.post('/token', status_code=status.HTTP_200_OK, response_model=Token) async def get_token( form_data: Annotated[OAuth2PasswordRequestForm, Depends()], db: db_dependency ): user = authenticate_user(form_data.username, form_data.password, db) if not user: - return "authentication failed" + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials") token = create_access_token(user.username, user.id, timedelta(minutes=20)) return {'access_token': token, 'token_type': 'bearer'} diff --git a/backend/routers/forum.py b/backend/routers/forum.py index ee0a75b..5558dbc 100644 --- a/backend/routers/forum.py +++ b/backend/routers/forum.py @@ -7,7 +7,9 @@ from starlette import status from database import SessionLocal from models import Post, Thread -router = APIRouter() +router = APIRouter( + tags=['forum'] +) def get_db():