feat: Complete backend API setup with registration endpoint
- Added user registration endpoint (/api/v1/auth/register) - Created MongoDB database connection module - Fixed user models to match frontend signup form - Exposed backend port 8000 for development - Configured Vite proxy for API requests - Successfully tested user registration flow Backend is now fully functional with: - MongoDB connection - User registration with password hashing - JWT token generation - Proper error handling 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
from fastapi import APIRouter, HTTPException, Depends, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from app.core.security import create_access_token, get_current_user
|
||||
from app.models.user import User
|
||||
from app.core.security import create_access_token, get_current_user, get_password_hash
|
||||
from app.models.user import User, UserCreate
|
||||
from app.core.config import settings
|
||||
from app.core.database import get_database
|
||||
from datetime import datetime
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@ -35,4 +37,49 @@ async def authorize():
|
||||
@router.post("/token")
|
||||
async def token():
|
||||
# TODO: Implement OAuth token endpoint
|
||||
return {"message": "Token endpoint"}
|
||||
return {"message": "Token endpoint"}
|
||||
|
||||
@router.post("/register", status_code=status.HTTP_201_CREATED)
|
||||
async def register(user_data: UserCreate):
|
||||
"""Register a new user"""
|
||||
# Get database
|
||||
db = get_database()
|
||||
# Check if user already exists
|
||||
users_collection = db["users"]
|
||||
existing_user = await users_collection.find_one({"email": user_data.email})
|
||||
|
||||
if existing_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Email already registered"
|
||||
)
|
||||
|
||||
# Create new user
|
||||
user_dict = {
|
||||
"email": user_data.email,
|
||||
"full_name": user_data.name,
|
||||
"username": user_data.email.split("@")[0], # Use email prefix as username
|
||||
"organization": user_data.organization,
|
||||
"hashed_password": get_password_hash(user_data.password),
|
||||
"role": "user", # Default role
|
||||
"is_active": True,
|
||||
"created_at": datetime.utcnow(),
|
||||
"updated_at": datetime.utcnow()
|
||||
}
|
||||
|
||||
# Insert user into database
|
||||
result = await users_collection.insert_one(user_dict)
|
||||
|
||||
# Create access token for immediate login
|
||||
access_token = create_access_token({"sub": user_data.email})
|
||||
|
||||
return {
|
||||
"message": "User registered successfully",
|
||||
"access_token": access_token,
|
||||
"token_type": "bearer",
|
||||
"user": {
|
||||
"id": str(result.inserted_id),
|
||||
"email": user_data.email,
|
||||
"name": user_data.name
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user