
- Phone number authentication with OTP verification - Email/password authentication with secure bcrypt hashing - Third-party OAuth login support for Google and Apple - JWT token-based authentication system - Rate limiting for OTP requests (5/minute) - SQLite database with SQLAlchemy ORM - Comprehensive user model with multiple auth providers - Alembic database migrations setup - API documentation with Swagger/OpenAPI - Health check and system endpoints - Environment configuration with security best practices - Code quality with Ruff linting and formatting Features: - POST /auth/request-otp - Request OTP for phone authentication - POST /auth/verify-otp - Verify OTP and get access token - POST /auth/signup-email - Email signup with password - POST /auth/login-email - Email login authentication - POST /auth/login-google - Google OAuth integration - POST /auth/login-apple - Apple OAuth integration - GET /user/me - Get current authenticated user info - GET / - API information and documentation links - GET /health - Application health check
43 lines
1.2 KiB
Python
43 lines
1.2 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.utils.auth import verify_token
|
|
from app.services.user import get_user_by_id
|
|
from app.models.user import User
|
|
|
|
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
|
|
payload = verify_token(token)
|
|
|
|
if payload is None:
|
|
raise credentials_exception
|
|
|
|
user_id = payload.get("sub")
|
|
if user_id is None:
|
|
raise credentials_exception
|
|
|
|
user = get_user_by_id(db, user_id=int(user_id))
|
|
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
|