add router tags

This commit is contained in:
agatha 2024-04-06 15:36:48 -04:00
parent ea4ae48b23
commit 24887274f0
2 changed files with 11 additions and 6 deletions

View File

@ -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'}

View File

@ -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():