feat: Update endpoint signup
This commit is contained in:
parent
8ce929445e
commit
b74f517afb
@ -1,13 +1,26 @@
|
|||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from pydantic import BaseModel
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import Optional
|
||||||
|
from database import get_db
|
||||||
|
from models import User
|
||||||
|
from schemas import UserCreate
|
||||||
|
from utils import hash_password
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class UserRegistration(BaseModel):
|
@router.post("/signup", response_model=UserCreate, status_code=status.HTTP_201_CREATED)
|
||||||
username: str
|
async def signup(user_data: UserCreate, db: Session = Depends(get_db)):
|
||||||
password: str
|
"""
|
||||||
email: str
|
Create a new user account
|
||||||
|
"""
|
||||||
|
existing_user = db.query(User).filter(User.email == user_data.email).first()
|
||||||
|
if existing_user:
|
||||||
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Email already registered")
|
||||||
|
|
||||||
@router.post("/signup")
|
hashed_password = hash_password(user_data.password)
|
||||||
async def signup(user: UserRegistration):
|
new_user = User(email=user_data.email, password=hashed_password)
|
||||||
return {"message": "User registered successfully", "user": user.username}
|
db.add(new_user)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(new_user)
|
||||||
|
|
||||||
|
return new_user
|
Loading…
x
Reference in New Issue
Block a user