Update code in endpoints/login.post.py
This commit is contained in:
parent
2be854520b
commit
4dde81e9fe
@ -1,23 +1,37 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from core.auth import get_current_user_dummy
|
|
||||||
from core.database import fake_users_db
|
from core.database import fake_users_db
|
||||||
|
from fastapi.security import OAuth2PasswordRequestForm
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/login")
|
@router.post("/login")
|
||||||
async def login_demo(
|
async def login_handler(
|
||||||
username: str = "demo",
|
form_data: OAuth2PasswordRequestForm = Depends()
|
||||||
password: str = "password"
|
|
||||||
):
|
):
|
||||||
"""Demo login endpoint"""
|
"""Authenticate user and return token"""
|
||||||
user = fake_users_db.get(username)
|
user = fake_users_db.get(form_data.username)
|
||||||
if not user or user["password"] != password:
|
|
||||||
raise HTTPException(status_code=400, detail="Invalid credentials")
|
if not user or user["password"] != form_data.password:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail="Incorrect username or password",
|
||||||
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
|
)
|
||||||
|
|
||||||
|
if user.get("disabled"):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail="Inactive user"
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"message": "Login successful (demo)",
|
"message": "Login successful",
|
||||||
"user": username,
|
"access_token": "dummy_jwt_token_" + form_data.username,
|
||||||
"token": "dummy_jwt_token_123",
|
"token_type": "bearer",
|
||||||
|
"user": {
|
||||||
|
"username": form_data.username,
|
||||||
|
"email": user["email"]
|
||||||
|
},
|
||||||
"features": {
|
"features": {
|
||||||
"rate_limit": 100,
|
"rate_limit": 100,
|
||||||
"expires_in": 3600
|
"expires_in": 3600
|
||||||
|
Loading…
x
Reference in New Issue
Block a user