
- Set up project structure and FastAPI application - Create database models with SQLAlchemy - Implement authentication with JWT - Add CRUD operations for products, inventory, categories - Implement purchase order and sales functionality - Create reporting endpoints - Set up Alembic for database migrations - Add comprehensive documentation in README.md
48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
from datetime import timedelta
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_db, get_current_user
|
|
from app.core.config import settings
|
|
from app.core.security.jwt import create_access_token
|
|
from app.core.security.password import verify_password
|
|
from app.crud.crud_user import user
|
|
from app.schemas.token import Token
|
|
from app.schemas.user import User
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/login/access-token", response_model=Token)
|
|
def login_access_token(
|
|
db: Session = Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
|
|
) -> Any:
|
|
"""
|
|
OAuth2 compatible token login, get an access token for future requests
|
|
"""
|
|
db_user = user.get_by_email(db, email=form_data.username)
|
|
if not db_user:
|
|
raise HTTPException(status_code=400, detail="Incorrect email or password")
|
|
if not verify_password(form_data.password, db_user.hashed_password):
|
|
raise HTTPException(status_code=400, detail="Incorrect email or password")
|
|
if not db_user.is_active:
|
|
raise HTTPException(status_code=400, detail="Inactive user")
|
|
|
|
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
return {
|
|
"access_token": create_access_token(
|
|
db_user.id, expires_delta=access_token_expires
|
|
),
|
|
"token_type": "bearer",
|
|
}
|
|
|
|
|
|
@router.post("/login/test-token", response_model=User)
|
|
def test_token(current_user: User = Depends(get_current_user)) -> Any:
|
|
"""
|
|
Test access token
|
|
"""
|
|
return current_user |