Update code in endpoints/signup.post.py
This commit is contained in:
parent
aa5c843a57
commit
e42f8ac430
@ -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
|
||||
access_token = create_access_token(data={"sub": user.email})
|
||||
|
||||
return {
|
||||
"access_token": access_token,
|
||||
"token_type": "bearer",
|
||||
"user": user
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user