Update code in endpoints/signup.post.py

This commit is contained in:
Backend IM Bot 2025-03-25 14:31:09 +00:00
parent 8c1ccaaa4d
commit e0ed8b5d8c

View File

@ -1,50 +1,27 @@
from fastapi import APIRouter, HTTPException, Depends from fastapi import APIRouter, HTTPException
from sqlalchemy.orm import Session
from pydantic import BaseModel users = [] # In-memory storage
from core.database import get_db
from core.auth import get_password_hash, create_access_token
import uuid
from models.user import User
router = APIRouter() router = APIRouter()
class UserCreate(BaseModel): @router.post("/reset-password")
username: str async def reset_password_demo(
email: str email: str = "user@example.com",
password: str new_password: str = "newpassword123"
@router.post("/signup")
async def signup(
user_data: UserCreate,
db: Session = Depends(get_db)
): ):
"""User registration endpoint""" """Demo reset password endpoint"""
# Check existing user user = next((u for u in users if u["email"] == email), None)
db_user = db.query(User).filter( if not user:
(User.username == user_data.username) | raise HTTPException(status_code=400, detail="Email not found")
(User.email == user_data.email)
).first()
if db_user:
raise HTTPException(
status_code=400,
detail="Username or email already exists"
)
# Create new user user["password"] = new_password
new_user = User(
id=str(uuid.uuid4()),
username=user_data.username,
email=user_data.email,
hashed_password=get_password_hash(user_data.password)
)
db.add(new_user)
db.commit()
# Return token directly after registration
return { return {
"message": "User created successfully", "message": "Password reset successful",
"access_token": create_access_token({"sub": new_user.id}), "user_id": user["id"],
"token_type": "bearer" "email": email,
} "next_steps": [
"Login with new password",
"Review security settings"
]
}