From 645ae33fa69e8834dc8652d8eb70d575683c89e3 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Wed, 12 Mar 2025 17:52:32 +0000 Subject: [PATCH] feat: Update endpoint login --- app/api/endpoints/login.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/api/endpoints/login.py b/app/api/endpoints/login.py index 6ed4dac..69c6d33 100644 --- a/app/api/endpoints/login.py +++ b/app/api/endpoints/login.py @@ -1,7 +1,23 @@ -from fastapi import APIRouter +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session +from typing import Optional +from models import User +from schemas import UserLogin, Token +from utils import get_db, authenticate_user, create_access_token router = APIRouter() -@router.post("/login") -async def login(username: str, password: str): - return {"message": "User logged in successfully", "username": username} \ No newline at end of file +@router.post("/login", response_model=Token) +async def login(user_credentials: UserLogin, db: Session = Depends(get_db)): + """ + Authenticate user and return access token + """ + user = authenticate_user(db, user_credentials.username, user_credentials.password) + if not user: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Incorrect username or password", + headers={"WWW-Authenticate": "Bearer"}, + ) + access_token = create_access_token(data={"sub": user.username}) + return {"access_token": access_token, "token_type": "bearer"} \ No newline at end of file