users table endpoints. auth to fix
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
from datetime import timedelta
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
from api.models import Token
|
||||
from api.utils import authenticate_user, create_access_token
|
||||
from db.internal import get_db_connection
|
||||
from settings import settings
|
||||
|
||||
auth_router = APIRouter(prefix="/api", tags=["auth"])
|
||||
|
||||
|
||||
@auth_router.post("/token")
|
||||
async def login(
|
||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
|
||||
conn: Annotated[connection, Depends(get_db_connection)]
|
||||
) -> Token:
|
||||
user = authenticate_user(conn, form_data.username, form_data.password) # change db
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
access_token_expire_time = timedelta(minutes=settings.access_token_expiration_time)
|
||||
access_token = create_access_token(
|
||||
data={"sub": user.username}, expires_delta=access_token_expire_time
|
||||
)
|
||||
return Token(access_token=access_token, token_type="bearer")
|
||||
Reference in New Issue
Block a user