Update code in endpoints/login.post.py
This commit is contained in:
parent
ec243902c8
commit
f6c465a9ef
@ -1,33 +1,43 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from core.database import fake_users_db
|
from core.database import fake_users_db
|
||||||
from core.auth import get_current_user_dummy
|
import uuid
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/login")
|
@router.post("/login")
|
||||||
async def login_handler(
|
async def login_handler(
|
||||||
username: str,
|
username: str = "demo",
|
||||||
password: str,
|
password: str = "password"
|
||||||
):
|
):
|
||||||
"""Authenticate user and return access token"""
|
"""Add predefined users and authenticate"""
|
||||||
|
# Add predefined users if they don't exist
|
||||||
|
if "Chidi" not in fake_users_db:
|
||||||
|
fake_users_db["Chidi"] = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
|
"email": "chidi@example.com",
|
||||||
|
"password": "chidi_password",
|
||||||
|
"disabled": False
|
||||||
|
}
|
||||||
|
|
||||||
|
if "Nneoma" not in fake_users_db:
|
||||||
|
fake_users_db["Nneoma"] = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
|
"email": "nneoma@example.com",
|
||||||
|
"password": "nneoma_password",
|
||||||
|
"disabled": False
|
||||||
|
}
|
||||||
|
|
||||||
user = fake_users_db.get(username)
|
user = fake_users_db.get(username)
|
||||||
if not user or user["password"] != password:
|
if not user or user["password"] != password:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=400, detail="Invalid credentials")
|
||||||
status_code=400,
|
|
||||||
detail="Incorrect username or password"
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"message": "Login successful",
|
"message": "Login successful (demo)",
|
||||||
"user": {
|
"user": username,
|
||||||
"username": username,
|
"token": "dummy_jwt_token_123",
|
||||||
"id": user["id"],
|
|
||||||
"email": user["email"]
|
|
||||||
},
|
|
||||||
"token": "dummy_jwt_token_" + username,
|
|
||||||
"features": {
|
"features": {
|
||||||
"rate_limit": 100,
|
"rate_limit": 100,
|
||||||
"expires_in": 3600,
|
"expires_in": 3600
|
||||||
"permissions": ["read", "write"]
|
},
|
||||||
}
|
"available_users": ["Chidi", "Nneoma"]
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user