Implement Phase 1: Session Management and Dashboard

This commit is contained in:
Jo Eskil
2025-11-13 17:08:24 +01:00
parent 382f0ec782
commit 90a40381ab
3 changed files with 67 additions and 5 deletions

13
auth.py
View File

@@ -4,12 +4,17 @@ from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session
import httpx
from database import get_db, User
from itsdangerous import URLSafeTimedSerializer
# IMPORTANT: These must be replaced with your actual Twitch application credentials
TWITCH_CLIENT_ID = "YOUR_TWITCH_CLIENT_ID"
TWITCH_CLIENT_SECRET = "YOUR_TWITCH_CLIENT_SECRET"
REDIRECT_URI = "http://localhost:8000/auth/twitch/callback"
# IMPORTANT: This should be a long, random string kept secret in a production environment
SECRET_KEY = "YOUR_SECRET_KEY"
serializer = URLSafeTimedSerializer(SECRET_KEY)
router = APIRouter()
@router.get("/login/twitch")
@@ -75,5 +80,9 @@ async def auth_twitch_callback(code: str, db: Session = Depends(get_db)):
user.twitch_refresh_token = refresh_token # TODO: Encrypt this
db.commit()
# TODO: Set a session cookie to keep the user logged in
return {"message": f"Successfully logged in as {twitch_username}"}
# Create a session cookie
response = RedirectResponse(url="/dashboard")
session_data = {"user_id": user.id}
session_cookie = serializer.dumps(session_data)
response.set_cookie(key="session", value=session_cookie)
return response