Update code in endpoints/login.post.py
This commit is contained in:
parent
768b79851d
commit
2d1d54de76
@ -1,37 +1,35 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from pydantic import BaseModel
|
import uuid
|
||||||
from datetime import timedelta
|
|
||||||
from core.database import get_db
|
cats = [] # In-memory storage
|
||||||
from sqlalchemy.orm import Session
|
|
||||||
from core.auth import verify_password, create_access_token
|
|
||||||
from models.user import User
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class UserAuth(BaseModel):
|
@router.post("/cats")
|
||||||
username: str
|
async def create_cat(
|
||||||
password: str
|
name: str = "Whiskers",
|
||||||
|
breed: str = "Persian",
|
||||||
@router.post("/login")
|
age: int = 2
|
||||||
async def login(
|
|
||||||
user_data: UserAuth,
|
|
||||||
db: Session = Depends(get_db)
|
|
||||||
):
|
):
|
||||||
"""User authentication endpoint"""
|
"""Demo cat creation endpoint"""
|
||||||
user = db.query(User).filter(User.username == user_data.username).first()
|
if any(c["name"] == name for c in cats):
|
||||||
|
raise HTTPException(status_code=400, detail="Cat already exists")
|
||||||
|
|
||||||
if not user or not verify_password(user_data.password, user.hashed_password):
|
cat_id = str(uuid.uuid4())
|
||||||
raise HTTPException(status_code=400, detail="Invalid credentials")
|
cats.append({
|
||||||
|
"id": cat_id,
|
||||||
# Generate token with expiration
|
"name": name,
|
||||||
access_token = create_access_token(
|
"breed": breed,
|
||||||
data={"sub": user.id},
|
"age": age,
|
||||||
expires_delta=timedelta(hours=1)
|
"disabled": False
|
||||||
)
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"access_token": access_token,
|
"message": "Cat created successfully",
|
||||||
"token_type": "bearer",
|
"cat_id": cat_id,
|
||||||
"user_id": user.id,
|
"name": name,
|
||||||
"username": user.username
|
"next_steps": [
|
||||||
|
"Schedule vet checkup",
|
||||||
|
"Complete vaccination records"
|
||||||
|
]
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user