Update code in endpoints/signup.post.py
This commit is contained in:
parent
2e588ff382
commit
6c75e9e6c4
@ -1,50 +1,35 @@
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from pydantic import BaseModel
|
||||
from core.database import get_db
|
||||
from core.auth import get_password_hash, create_access_token
|
||||
from fastapi import APIRouter, HTTPException
|
||||
import uuid
|
||||
from models.user import User
|
||||
|
||||
contacts = [] # In-memory storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
username: str
|
||||
email: str
|
||||
password: str
|
||||
|
||||
@router.post("/signup")
|
||||
async def signup(
|
||||
user_data: UserCreate,
|
||||
db: Session = Depends(get_db)
|
||||
async def signup_ngo(
|
||||
name: str = "Organization Name",
|
||||
email: str = "org@example.com",
|
||||
phone: str = "123-456-7890"
|
||||
):
|
||||
"""User registration endpoint"""
|
||||
# Check existing user
|
||||
db_user = db.query(User).filter(
|
||||
(User.username == user_data.username) |
|
||||
(User.email == user_data.email)
|
||||
).first()
|
||||
|
||||
if db_user:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Username or email already exists"
|
||||
)
|
||||
"""NGO signup endpoint"""
|
||||
if any(c["email"] == email for c in contacts):
|
||||
raise HTTPException(status_code=400, detail="Email already registered")
|
||||
|
||||
# Create new user
|
||||
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()
|
||||
contact_id = str(uuid.uuid4())
|
||||
contacts.append({
|
||||
"id": contact_id,
|
||||
"name": name,
|
||||
"email": email,
|
||||
"phone": phone,
|
||||
"status": "pending"
|
||||
})
|
||||
|
||||
# Return token directly after registration
|
||||
return {
|
||||
"message": "User created successfully",
|
||||
"access_token": create_access_token({"sub": new_user.id}),
|
||||
"token_type": "bearer"
|
||||
}
|
||||
"message": "Contact request submitted successfully",
|
||||
"contact_id": contact_id,
|
||||
"name": name,
|
||||
"next_steps": [
|
||||
"Check email for confirmation",
|
||||
"Schedule orientation call"
|
||||
]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user