Update code in endpoints/login.post.py

This commit is contained in:
Backend IM Bot 2025-03-19 19:15:31 +00:00
parent 4dde81e9fe
commit 9bf6e2b0d2

View File

@ -1,39 +1,44 @@
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 fastapi.security import OAuth2PasswordRequestForm from typing import List, Optional
router = APIRouter() router = APIRouter()
@router.post("/login") # Simulated playlist database
async def login_handler( fake_playlists_db = {
form_data: OAuth2PasswordRequestForm = Depends() "playlist1": {
"id": "playlist1",
"name": "My Favorites",
"songs": [
{"id": "song1", "title": "Song 1", "artist": "Artist 1", "duration": "3:45"},
{"id": "song2", "title": "Song 2", "artist": "Artist 2", "duration": "4:20"},
]
}
}
@router.post("/playlist/songs")
async def get_playlist_songs(
playlist_id: str,
limit: Optional[int] = 10,
offset: Optional[int] = 0
): ):
"""Authenticate user and return token""" """Fetch list of songs from a playlist"""
user = fake_users_db.get(form_data.username) if playlist_id not in fake_playlists_db:
raise HTTPException(status_code=404, detail="Playlist not found")
playlist = fake_playlists_db[playlist_id]
songs = playlist["songs"][offset:offset + limit]
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", "message": "Songs fetched successfully",
"access_token": "dummy_jwt_token_" + form_data.username, "data": {
"token_type": "bearer", "playlist_name": playlist["name"],
"user": { "songs": songs,
"username": form_data.username, "total_songs": len(playlist["songs"])
"email": user["email"]
}, },
"features": { "metadata": {
"rate_limit": 100, "limit": limit,
"expires_in": 3600 "offset": offset,
"has_more": (offset + limit) < len(playlist["songs"])
} }
} }