Update code in endpoints/signup.post.py
This commit is contained in:
parent
dcbf126bbc
commit
cd713a84b6
@ -2,27 +2,22 @@ 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 UserLogin, UserResponse
|
from schemas.user import UserCreate, UserResponse
|
||||||
from helpers.auth_helpers import authenticate_user, create_access_token
|
from helpers.user_helpers import create_user, get_user_by_email
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/signup", status_code=status.HTTP_200_OK, response_model=UserResponse)
|
@router.post("/signup", status_code=status.HTTP_201_CREATED, response_model=UserResponse)
|
||||||
async def login(
|
async def signup(
|
||||||
user_data: UserLogin,
|
user_data: UserCreate,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
user = authenticate_user(db, user_data.email, user_data.password)
|
existing_user = get_user_by_email(db, user_data.email)
|
||||||
if not user:
|
if existing_user:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail="Incorrect email or password",
|
detail="Email already registered"
|
||||||
headers={"WWW-Authenticate": "Bearer"},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
access_token = create_access_token(data={"sub": user.email})
|
new_user = create_user(db, user_data)
|
||||||
return {
|
return new_user
|
||||||
"access_token": access_token,
|
|
||||||
"token_type": "bearer",
|
|
||||||
"user": user
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user