
Features: - User registration and authentication with JWT tokens - Multi-level admin access (Admin and Super Admin) - Gym management with membership plans - Subscription management with payment integration - Stripe and Paystack payment gateway support - Role-based access control - SQLite database with Alembic migrations - Comprehensive API endpoints with FastAPI - Database models for users, gyms, memberships, subscriptions, and transactions - Admin endpoints for user management and financial reporting - Health check and documentation endpoints Core Components: - FastAPI application with CORS support - SQLAlchemy ORM with relationship mapping - JWT-based authentication with bcrypt password hashing - Payment service abstraction for multiple gateways - Pydantic schemas for request/response validation - Alembic database migration system - Admin dashboard functionality - Environment variable configuration
55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
from datetime import timedelta
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
from app.core import security
|
|
from app.core.config import settings
|
|
from app.models.user import User
|
|
from app.schemas.user import Token, UserCreate, User as UserSchema, UserLogin
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/register", response_model=UserSchema)
|
|
def register(user_data: UserCreate, db: Session = Depends(get_db)):
|
|
db_user = db.query(User).filter(User.email == user_data.email).first()
|
|
if db_user:
|
|
raise HTTPException(status_code=400, detail="Email already registered")
|
|
|
|
hashed_password = security.get_password_hash(user_data.password)
|
|
db_user = User(
|
|
email=user_data.email,
|
|
full_name=user_data.full_name,
|
|
phone=user_data.phone,
|
|
hashed_password=hashed_password,
|
|
)
|
|
db.add(db_user)
|
|
db.commit()
|
|
db.refresh(db_user)
|
|
return db_user
|
|
|
|
|
|
@router.post("/login", response_model=Token)
|
|
def login(user_credentials: UserLogin, db: Session = Depends(get_db)):
|
|
user = db.query(User).filter(User.email == user_credentials.email).first()
|
|
|
|
if not user or not security.verify_password(
|
|
user_credentials.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=400, detail="Inactive user")
|
|
|
|
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
access_token = security.create_access_token(
|
|
data={"sub": user.email}, expires_delta=access_token_expires
|
|
)
|
|
|
|
return {"access_token": access_token, "token_type": "bearer"}
|