- Add findagram.co React frontend with product search, brands, categories - Add findadispo.com React frontend with dispensary locator - Wire findagram to backend /api/az/* endpoints - Update category/brand links to route to /products with filters - Add k8s manifests for both frontends - Add multi-domain user support migrations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from passlib.context import CryptContext
|
|
from jose import jwt
|
|
from datetime import datetime, timedelta
|
|
|
|
from app.database import get_db
|
|
from app.config import get_settings
|
|
from app.models.user import User
|
|
from app.schemas.user import UserCreate, UserLogin, UserResponse, Token
|
|
|
|
router = APIRouter()
|
|
settings = get_settings()
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
|
|
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
|
return pwd_context.verify(plain_password, hashed_password)
|
|
|
|
|
|
def get_password_hash(password: str) -> str:
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
def create_access_token(data: dict) -> str:
|
|
to_encode = data.copy()
|
|
expire = datetime.utcnow() + timedelta(minutes=settings.access_token_expire_minutes)
|
|
to_encode.update({"exp": expire})
|
|
return jwt.encode(to_encode, settings.secret_key, algorithm=settings.algorithm)
|
|
|
|
|
|
@router.post("/register", response_model=Token)
|
|
async def register(user_data: UserCreate, db: AsyncSession = Depends(get_db)):
|
|
# Check if user exists
|
|
result = await db.execute(select(User).where(User.email == user_data.email))
|
|
if result.scalar_one_or_none():
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Email already registered"
|
|
)
|
|
|
|
# Create new user
|
|
hashed_password = get_password_hash(user_data.password)
|
|
user = User(
|
|
email=user_data.email,
|
|
hashed_password=hashed_password,
|
|
name=user_data.name,
|
|
)
|
|
db.add(user)
|
|
await db.commit()
|
|
await db.refresh(user)
|
|
|
|
# Create access token
|
|
access_token = create_access_token({"sub": str(user.id)})
|
|
return Token(access_token=access_token)
|
|
|
|
|
|
@router.post("/login", response_model=Token)
|
|
async def login(user_data: UserLogin, db: AsyncSession = Depends(get_db)):
|
|
result = await db.execute(select(User).where(User.email == user_data.email))
|
|
user = result.scalar_one_or_none()
|
|
|
|
if not user or not verify_password(user_data.password, user.hashed_password):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid credentials"
|
|
)
|
|
|
|
if not user.is_active:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="User account is disabled"
|
|
)
|
|
|
|
access_token = create_access_token({"sub": str(user.id)})
|
|
return Token(access_token=access_token)
|