Update code in endpoints/login.post.py

This commit is contained in:
Backend IM Bot 2025-03-23 18:54:11 +00:00
parent 973a5dafbd
commit 73e75e8fe1

View File

@ -1,37 +1,35 @@
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from datetime import timedelta
from core.database import get_db
from sqlalchemy.orm import Session
from core.auth import verify_password, create_access_token
from models.user import User
from fastapi import APIRouter, HTTPException
import uuid
todos = [] # In-memory storage
router = APIRouter()
class UserAuth(BaseModel):
username: str
password: str
@router.post("/login")
async def login(
user_data: UserAuth,
db: Session = Depends(get_db)
@router.post("/todos")
async def create_todo(
title: str = "New Todo",
description: str = "Todo description",
priority: int = 1
):
"""User authentication endpoint"""
user = db.query(User).filter(User.username == user_data.username).first()
"""Create a new todo item"""
todo_id = str(uuid.uuid4())
if not user or not verify_password(user_data.password, user.hashed_password):
raise HTTPException(status_code=400, detail="Invalid credentials")
# Generate token with expiration
access_token = create_access_token(
data={"sub": user.id},
expires_delta=timedelta(hours=1)
)
return {
"access_token": access_token,
"token_type": "bearer",
"user_id": user.id,
"username": user.username
todo = {
"id": todo_id,
"title": title,
"description": description,
"priority": priority,
"completed": False
}
todos.append(todo)
return {
"message": "Todo created successfully",
"todo_id": todo_id,
"title": title,
"next_steps": [
"Add more todos",
"Mark as complete when done"
]
}