start
This commit is contained in:
23
backend/app/routers/auth.py
Normal file
23
backend/app/routers/auth.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
|
||||
from app.core.dependencies import DbSession
|
||||
from app.core.security import create_access_token
|
||||
from app.schemas.auth import Token
|
||||
from app.services.auth import authenticate_user
|
||||
|
||||
router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
|
||||
|
||||
|
||||
@router.post("/token", response_model=Token)
|
||||
async def login(db: DbSession, form_data: Annotated[OAuth2PasswordRequestForm, Depends()]) -> Token:
|
||||
user = await authenticate_user(db, form_data.username, form_data.password)
|
||||
if user is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
return Token(access_token=create_access_token(user.id))
|
||||
Reference in New Issue
Block a user