
- Set up FastAPI project structure with modular architecture - Create comprehensive database models for users, properties, messages, notifications, and payments - Implement JWT-based authentication with role-based access control (Seeker, Agent, Landlord, Admin) - Build property listings CRUD with advanced search and filtering capabilities - Add dedicated affordable housing endpoints for Nigerian market focus - Create real-time messaging system between users - Implement admin dashboard with property approval workflow and analytics - Add notification system for user alerts - Integrate Paystack payment gateway for transactions - Set up SQLite database with Alembic migrations - Include comprehensive health check and API documentation - Add proper error handling and validation throughout - Follow FastAPI best practices with Pydantic schemas and dependency injection
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
from datetime import timedelta
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from app.auth.dependencies import get_db
|
|
from app.core.security import verify_password, get_password_hash, create_access_token, ACCESS_TOKEN_EXPIRE_MINUTES
|
|
from app.models.user import User
|
|
from app.models.profile import Profile
|
|
from app.schemas.user import UserCreate, UserLogin, UserResponse, Token
|
|
|
|
router = APIRouter(prefix="/api/auth", tags=["Authentication"])
|
|
|
|
|
|
@router.post("/register", response_model=UserResponse)
|
|
def register(user: UserCreate, db: Session = Depends(get_db)):
|
|
# Check if user already exists
|
|
if db.query(User).filter(User.email == user.email).first():
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Email already registered"
|
|
)
|
|
|
|
if db.query(User).filter(User.username == user.username).first():
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Username already taken"
|
|
)
|
|
|
|
if db.query(User).filter(User.phone_number == user.phone_number).first():
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Phone number already registered"
|
|
)
|
|
|
|
# Create new user
|
|
hashed_password = get_password_hash(user.password)
|
|
db_user = User(
|
|
username=user.username,
|
|
email=user.email,
|
|
phone_number=user.phone_number,
|
|
hashed_password=hashed_password,
|
|
role=user.role
|
|
)
|
|
db.add(db_user)
|
|
db.commit()
|
|
db.refresh(db_user)
|
|
|
|
# Create user profile
|
|
profile = Profile(user_id=db_user.id)
|
|
db.add(profile)
|
|
db.commit()
|
|
|
|
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 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=status.HTTP_400_BAD_REQUEST,
|
|
detail="Inactive user"
|
|
)
|
|
|
|
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
access_token = create_access_token(
|
|
data={"sub": user.username}, expires_delta=access_token_expires
|
|
)
|
|
|
|
return {"access_token": access_token, "token_type": "bearer"} |