Update code in endpoints/signup.post.py

This commit is contained in:
Backend IM Bot 2025-03-28 18:07:25 +00:00
parent aa5c843a57
commit e42f8ac430

View File

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