
- Setup project structure and FastAPI application - Create SQLite database with SQLAlchemy - Implement user authentication with JWT - Create task and user models - Add CRUD operations for tasks and users - Configure Alembic for database migrations - Implement API endpoints for task management - Add error handling and validation - Configure CORS middleware - Create health check endpoint - Add comprehensive documentation
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
from datetime import timedelta
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core import auth
|
|
from app.core.config import settings
|
|
from app.core.exceptions import InactiveUserException, InvalidCredentialsException
|
|
from app.core.security import create_access_token
|
|
from app.db.session import get_db
|
|
from app.schemas.token import Token
|
|
from app.schemas.user import User
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/login", response_model=Token)
|
|
async def login_access_token(
|
|
db: AsyncSession = Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
|
|
) -> Any:
|
|
"""
|
|
OAuth2 compatible token login, get an access token for future requests
|
|
"""
|
|
user = await auth.authenticate(
|
|
db, email=form_data.username, password=form_data.password
|
|
)
|
|
if not user:
|
|
raise InvalidCredentialsException()
|
|
if not user.is_active:
|
|
raise InactiveUserException()
|
|
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
return {
|
|
"access_token": create_access_token(
|
|
user.id, expires_delta=access_token_expires
|
|
),
|
|
"token_type": "bearer",
|
|
}
|
|
|
|
|
|
@router.post("/test-token", response_model=User)
|
|
async def test_token(current_user: User = Depends(auth.get_current_user)) -> Any:
|
|
"""
|
|
Test access token
|
|
"""
|
|
return current_user |