31 lines
No EOL
1.2 KiB
Python
31 lines
No EOL
1.2 KiB
Python
from datetime import timedelta
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
from starlette import status
|
|
|
|
from authentication import authenticate_user, create_access_token, get_current_active_user
|
|
from config import ACCESS_TOKEN_EXPIRE_MINUTES, get_fake_users_db
|
|
from models import Token, User
|
|
|
|
router = APIRouter()
|
|
|
|
# Update the token endpoint in your main.py or wherever it's defined
|
|
@router.post("/token", response_model=Token)
|
|
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
try:
|
|
user = authenticate_user(get_fake_users_db(), form_data.username, form_data.password)
|
|
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
access_token = create_access_token(
|
|
data={"sub": user.username}, expires_delta=access_token_expires
|
|
)
|
|
return {"access_token": access_token, "token_type": "bearer"}
|
|
except HTTPException as e:
|
|
raise e
|
|
|
|
@router.get("/users/me")
|
|
async def read_users_me(
|
|
current_user: Annotated[User, Depends(get_current_active_user)],
|
|
):
|
|
return current_user |