add router tags
This commit is contained in:
parent
ea4ae48b23
commit
24887274f0
@ -11,13 +11,16 @@ from jose import jwt, JWTError
|
|||||||
from models import User
|
from models import User
|
||||||
from database import SessionLocal
|
from database import SessionLocal
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter(
|
||||||
|
prefix='/auth',
|
||||||
|
tags=['auth']
|
||||||
|
)
|
||||||
|
|
||||||
SECRET_KEY = '3b004eeae34b43bd05226f210d9bdc2ad99abdd3c52bf32802906085b762ff55'
|
SECRET_KEY = '3b004eeae34b43bd05226f210d9bdc2ad99abdd3c52bf32802906085b762ff55'
|
||||||
ALGORITHM = 'HS256'
|
ALGORITHM = 'HS256'
|
||||||
|
|
||||||
bcrypt_context = CryptContext(schemes=['bcrypt'], deprecated='auto')
|
bcrypt_context = CryptContext(schemes=['bcrypt'], deprecated='auto')
|
||||||
oauth2_bearer = OAuth2PasswordBearer(tokenUrl='validate')
|
oauth2_bearer = OAuth2PasswordBearer(tokenUrl='auth/token')
|
||||||
|
|
||||||
|
|
||||||
def get_db():
|
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")
|
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):
|
async def create_user(db: db_dependency, data: CreateUser):
|
||||||
create_user_model = User(
|
create_user_model = User(
|
||||||
username=data.username,
|
username=data.username,
|
||||||
@ -85,14 +88,14 @@ async def create_user(db: db_dependency, data: CreateUser):
|
|||||||
db.commit()
|
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(
|
async def get_token(
|
||||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
|
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
|
||||||
db: db_dependency
|
db: db_dependency
|
||||||
):
|
):
|
||||||
user = authenticate_user(form_data.username, form_data.password, db)
|
user = authenticate_user(form_data.username, form_data.password, db)
|
||||||
if not user:
|
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))
|
token = create_access_token(user.username, user.id, timedelta(minutes=20))
|
||||||
return {'access_token': token, 'token_type': 'bearer'}
|
return {'access_token': token, 'token_type': 'bearer'}
|
||||||
|
@ -7,7 +7,9 @@ from starlette import status
|
|||||||
from database import SessionLocal
|
from database import SessionLocal
|
||||||
from models import Post, Thread
|
from models import Post, Thread
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter(
|
||||||
|
tags=['forum']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
|
Loading…
Reference in New Issue
Block a user