Update code in endpoints/signup.post.py

This commit is contained in:
Backend IM Bot 2025-03-29 21:57:34 +00:00
parent dcbf126bbc
commit cd713a84b6

View File

@ -2,27 +2,22 @@ from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from core.database import get_db
from models.user import User
from schemas.user import UserLogin, UserResponse
from helpers.auth_helpers import authenticate_user, create_access_token
from schemas.user import UserCreate, UserResponse
from helpers.user_helpers import create_user, get_user_by_email
router = APIRouter()
@router.post("/signup", status_code=status.HTTP_200_OK, response_model=UserResponse)
async def login(
user_data: UserLogin,
@router.post("/signup", status_code=status.HTTP_201_CREATED, response_model=UserResponse)
async def signup(
user_data: UserCreate,
db: Session = Depends(get_db)
):
user = authenticate_user(db, user_data.email, user_data.password)
if not user:
existing_user = get_user_by_email(db, user_data.email)
if existing_user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Bearer"},
status_code=status.HTTP_400_BAD_REQUEST,
detail="Email already registered"
)
access_token = create_access_token(data={"sub": user.email})
return {
"access_token": access_token,
"token_type": "bearer",
"user": user
}
new_user = create_user(db, user_data)
return new_user