Added cognito authentication - Fix 8
All checks were successful
AWS Deploy on Push / build (push) Successful in 1m13s

This commit is contained in:
2025-05-15 16:52:54 -05:00
parent ae040fc49e
commit 30ccf86c86
2 changed files with 56 additions and 45 deletions

View File

@@ -1,22 +1,12 @@
from fastapi import FastAPI, Depends, HTTPException, Request, Response
from fastapi.responses import RedirectResponse, JSONResponse
from fastapi import FastAPI, Depends, HTTPException, Request
from fastapi.responses import RedirectResponse
from app.cabletv.utils.auth import get_current_user, exchange_code_for_token
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add session middleware
app.add_middleware(SessionMiddleware, secret_key="your-secret-key")
@app.get("/")
async def root():
return {"message": "IPTV Updater API"}
@app.get("/protected")
async def protected_route(request: Request, user = Depends(get_current_user)):
@@ -28,17 +18,10 @@ async def auth_callback(request: Request, code: str):
redirect_uri = str(request.base_url)
tokens = exchange_code_for_token(code, redirect_uri)
# For browser requests, redirect to protected page
is_browser = "text/html" in request.headers.get("accept", "")
if is_browser:
response = RedirectResponse(url="/protected")
else:
response = JSONResponse(content={
"message": "Authentication successful",
"id_token": tokens["id_token"]
})
# Create redirect response to protected route
response = RedirectResponse(url="/protected", status_code=302)
# Set the token cookie
# Set token cookie
response.set_cookie(
key="token",
value=tokens["id_token"],