From e42f8ac430300a719c9c52b7b85b38fa33a90bf8 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Fri, 28 Mar 2025 18:07:25 +0000 Subject: [PATCH] Update code in endpoints/signup.post.py --- endpoints/signup.post.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/endpoints/signup.post.py b/endpoints/signup.post.py index 2a692ce..9bed033 100644 --- a/endpoints/signup.post.py +++ b/endpoints/signup.post.py @@ -2,22 +2,28 @@ 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 UserCreate, UserResponse -from helpers.user_helpers import create_user, get_user_by_email +from schemas.user import UserLogin, UserResponse +from helpers.auth_helpers import authenticate_user, create_access_token router = APIRouter() -@router.post("/signup", status_code=status.HTTP_201_CREATED, response_model=UserResponse) -async def signup( - user_data: UserCreate, +@router.post("/login", response_model=UserResponse) +async def login( + user_credentials: UserLogin, db: Session = Depends(get_db) ): - existing_user = get_user_by_email(db, user_data.email) - if existing_user: + user = authenticate_user(db, user_credentials.email, user_credentials.password) + if not user: raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="Email already registered" + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Incorrect email or password", + headers={"WWW-Authenticate": "Bearer"}, ) - new_user = create_user(db, user_data) - return new_user \ No newline at end of file + access_token = create_access_token(data={"sub": user.email}) + + return { + "access_token": access_token, + "token_type": "bearer", + "user": user + } \ No newline at end of file