Update code in endpoints/login.post.py
This commit is contained in:
parent
2f594be89d
commit
4bb7f8e83c
@ -1,37 +1,25 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from pydantic import BaseModel
|
|
||||||
from datetime import timedelta
|
users = [] # In-memory storage
|
||||||
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
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class UserAuth(BaseModel):
|
|
||||||
username: str
|
|
||||||
password: str
|
|
||||||
|
|
||||||
@router.post("/login")
|
@router.post("/login")
|
||||||
async def login(
|
async def login_demo(
|
||||||
user_data: UserAuth,
|
username: str = "demo",
|
||||||
db: Session = Depends(get_db)
|
password: str = "password"
|
||||||
):
|
):
|
||||||
"""User authentication endpoint"""
|
"""Demo login endpoint"""
|
||||||
user = db.query(User).filter(User.username == user_data.username).first()
|
user = next((u for u in users if u["username"] == username), None)
|
||||||
|
if not user or user["password"] != password:
|
||||||
if not user or not verify_password(user_data.password, user.hashed_password):
|
|
||||||
raise HTTPException(status_code=400, detail="Invalid credentials")
|
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 {
|
return {
|
||||||
"access_token": access_token,
|
"message": "Login successful (demo)",
|
||||||
"token_type": "bearer",
|
"user": username,
|
||||||
"user_id": user.id,
|
"token": "dummy_jwt_token_123",
|
||||||
"username": user.username
|
"features": {
|
||||||
|
"rate_limit": 100,
|
||||||
|
"expires_in": 3600
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user