Automated Action 9c818fbd91 Implement complete user authentication system with FastAPI
- Set up FastAPI application with CORS and proper structure
- Created User model with SQLAlchemy and SQLite database
- Implemented JWT-based authentication with bcrypt password hashing
- Added user registration, login, and profile endpoints
- Created health check endpoint for monitoring
- Set up Alembic for database migrations
- Added comprehensive API documentation
- Configured proper project structure with separate modules
- Updated README with complete setup and usage instructions
2025-06-25 01:56:41 +00:00

34 lines
1.1 KiB
Python

from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlalchemy.orm import Session
from app.db.session import get_db
from app.models.user import User
from app.core.security import verify_token
security = HTTPBearer()
def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(security),
db: Session = Depends(get_db)
) -> User:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
token = credentials.credentials
email = verify_token(token)
if email is None:
raise credentials_exception
user = db.query(User).filter(User.email == email).first()
if user is None:
raise credentials_exception
return user
def get_current_active_user(current_user: User = Depends(get_current_user)) -> User:
if not current_user.is_active:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user