Spaces:
Runtime error
Runtime error
""" | |
Router for handling user authentication and issuing JWT tokens. | |
""" | |
from fastapi import APIRouter, Depends, HTTPException, status | |
from fastapi.security import OAuth2PasswordRequestForm | |
from fastapi.concurrency import run_in_threadpool | |
from sqlalchemy.orm import Session | |
from datetime import timedelta | |
from src import models | |
from src.database import get_db | |
from src.auth import create_access_token, authenticate_user | |
from src.config import settings | |
router = APIRouter( | |
prefix="/api", | |
tags=["Authentication Token"] | |
) | |
async def login_for_access_token( | |
form_data: OAuth2PasswordRequestForm = Depends(), | |
db: Session = Depends(get_db) | |
): | |
""" | |
Provides a JWT token for authenticated users. | |
This is the primary login endpoint. It takes a username and password | |
and returns an access token if the credentials are valid. | |
""" | |
user = authenticate_user(db, form_data.username, form_data.password) | |
if not user: | |
raise HTTPException( | |
status_code=status.HTTP_401_UNAUTHORIZED, | |
detail="Incorrect username or password", | |
headers={"WWW-Authenticate": "Bearer"}, | |
) | |
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) | |
access_token = create_access_token( | |
data={"sub": user.username, "role": user.role.value}, | |
expires_delta=access_token_expires | |
) | |
return {"access_token": access_token, "token_type": "bearer"} | |