JobSpy/api/v1/token/__init__.py

24 lines
848 B
Python
Raw Normal View History

2023-07-09 13:15:39 -07:00
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from api.core.users import Token
from .db_utils import authenticate_user
from .auth import create_access_token
ACCESS_TOKEN_EXPIRE_MINUTES = 30
router = APIRouter(prefix="/token")
@router.post("/", response_model=Token)
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
user = authenticate_user(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"},
)
access_token = create_access_token(data={"sub": user.username})
return {"access_token": access_token, "token_type": "bearer"}