From 18ea09a987fff44f8c154317b15cbb64fb5bf9b4 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Fri, 28 Mar 2025 04:36:05 +0000 Subject: [PATCH] Add POST endpoint for /login --- endpoints/login.post.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/endpoints/login.post.py b/endpoints/login.post.py index 1f11512..1d0b522 100644 --- a/endpoints/login.post.py +++ b/endpoints/login.post.py @@ -1,33 +1,34 @@ # Entity: User +```python 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 UserSchema, UserLogin +from schemas.user import UserSchema, UserLogin, UserResponse from helpers.user_helpers import authenticate_user, create_access_token -from core.security import create_access_token +from core.security import verify_password router = APIRouter() -@router.post("/login", status_code=200) +@router.post("/login", response_model=UserResponse, status_code=status.HTTP_200_OK) async def login( - login_data: UserLogin, + user_credentials: UserLogin, db: Session = Depends(get_db) ): - """Authenticate user and return access token""" - user = authenticate_user(db, login_data.username, login_data.password) + user = authenticate_user(db, user_credentials.email, user_credentials.password) if not user: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, - detail="Incorrect username or password", + detail="Incorrect email or password", headers={"WWW-Authenticate": "Bearer"}, ) - access_token = create_access_token(data={"sub": user.username}) + access_token = create_access_token(data={"sub": user.email}) return { "access_token": access_token, "token_type": "bearer", - "user": UserSchema.from_orm(user) - } \ No newline at end of file + "user": user + } +``` \ No newline at end of file