Update code in endpoints/login.post.py

This commit is contained in:
Backend IM Bot 2025-03-28 19:07:49 +00:00
parent d5fe8809b2
commit 84b088ce58

View File

@ -2,20 +2,29 @@ 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 UserLoginSchema, UserResponse
from helpers.auth_helpers import validate_user_credentials
from schemas.user import UserLogin, UserResponse
from helpers.auth_helpers import authenticate_user
from helpers.token_helpers import create_access_token
router = APIRouter()
@router.post("/login", status_code=200, response_model=UserResponse)
async def login(
credentials: UserLoginSchema,
login_data: UserLogin,
db: Session = Depends(get_db)
):
user = validate_user_credentials(db, credentials.userId, credentials.password)
user = authenticate_user(db, login_data.userid, login_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid credentials"
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
return user
access_token = create_access_token(data={"sub": user.userid})
return {
"access_token": access_token,
"token_type": "bearer",
"userid": user.userid
}