aboutsummaryrefslogtreecommitdiff
path: root/app/routes/refresh_route.py
blob: a8b7a5c08d90daf18d567664a03de66927040045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from fastapi import Depends, APIRouter
from datetime import timedelta
from typing import Annotated

from app.util.authentication import (
    create_access_token,
    refresh_get_current_user,
)
from app.schemas.auth_schemas import Token, User


router = APIRouter(prefix="/refresh", tags=["refresh"])


# Full native JWT support is not complete in FastAPI yet :(
# Part of that is token refresh, so we must implement it ourselves
@router.post("/")
async def refresh_access_token(
    current_user: Annotated[User, Depends(refresh_get_current_user)],
) -> Token:
    """
    Return a new access token if the refresh token is valid
    """
    access_token_expires = timedelta(minutes=30)
    access_token = create_access_token(
        data={"sub": current_user.username, "refresh": False},
        expires_delta=access_token_expires,
    )
    return Token(
        access_token=access_token,
        token_type="bearer",
    )