
Features include: - User management with JWT authentication and role-based access - Inventory items with SKU/barcode tracking and stock management - Categories and suppliers organization - Inventory transactions with automatic stock updates - Low stock alerts and advanced search/filtering - RESTful API with comprehensive CRUD operations - SQLite database with Alembic migrations - Auto-generated API documentation Co-Authored-By: Claude <noreply@anthropic.com>
31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
from datetime import timedelta
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
from sqlalchemy.orm import Session
|
|
from app.core.config import settings
|
|
from app.db.session import get_db
|
|
from app.crud import user
|
|
from app.schemas.auth import Token
|
|
from app.auth.auth_handler import create_access_token
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/login", response_model=Token)
|
|
def login_for_access_token(
|
|
db: Session = Depends(get_db),
|
|
form_data: OAuth2PasswordRequestForm = Depends()
|
|
):
|
|
user_obj = user.authenticate(
|
|
db, email=form_data.username, password=form_data.password
|
|
)
|
|
if not user_obj:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Incorrect email or password",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
access_token = create_access_token(
|
|
data={"sub": user_obj.email}, expires_delta=access_token_expires
|
|
)
|
|
return {"access_token": access_token, "token_type": "bearer"} |