
- Implemented complete authentication system with JWT tokens - Created user management with registration and profile endpoints - Built client management with full CRUD operations - Developed invoice system with line items and automatic calculations - Set up SQLite database with proper migrations using Alembic - Added health monitoring and API documentation - Configured CORS for cross-origin requests - Included comprehensive README with usage examples
36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
from sqlalchemy.orm import Session
|
|
from datetime import timedelta
|
|
from app.db.session import get_db
|
|
from app.core.security import verify_password, create_access_token
|
|
from app.core.config import settings
|
|
from app.models.user import User
|
|
from app.schemas.user import Token, UserLogin
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/login", response_model=Token)
|
|
async def login_for_access_token(
|
|
user_login: UserLogin,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
user = db.query(User).filter(User.email == user_login.email).first()
|
|
if not user or not verify_password(user_login.password, user.hashed_password):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Incorrect email or password",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
if not user.is_active:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Inactive user"
|
|
)
|
|
|
|
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
access_token = create_access_token(
|
|
data={"sub": user.email}, expires_delta=access_token_expires
|
|
)
|
|
return {"access_token": access_token, "token_type": "bearer"} |