12 Shiny + FastAPI Authentication Tutorial: Display Protected Images with Session Cookies

In this tutorial, we connect a Shiny for Python frontend to a FastAPI backend and display protected content using session-based authentication. Source code and free course. https://industry-python.thinkific.com/ Previously, we secured a coupon image behind a FastAPI endpoint. Now we'll build the frontend that logs in, communicates with the API using HTTPX, and displays the protected image only for authenticated users. Topics covered: Shiny for Python authentication UI Connecting Shiny to FastAPI HTTPX client sessions Session cookie authentication Protected image downloads Displaying images with Pillow Using reactive values in Shiny Environment-based API configuration Error handling with HTTPX Multi-user authentication testing Architecture: Browser ↓ Shiny Frontend ↓ HTTPX + Session Cookies FastAPI Backend ↓ Protected Coupon Image What you'll build: ✅ Login form with username and password ✅ FastAPI authentication using session cookies ✅ Protected image endpoint ✅ User-specific authentication state ✅ Error handling for invalid credentials ✅ Dynamic image rendering from API responses An important security improvement in this lesson is removing the image from the frontend application and serving it exclusively through FastAPI. The protected content now lives on the API server and is only accessible after authentication succeeds. You'll also learn how to: Use HTTPX to maintain authenticated sessions Retrieve protected content from FastAPI Render images using Pillow Handle 200, 204, and 401 responses Test multiple authenticated users For demonstration purposes, users are stored in a password file. In future lessons we'll replace this with a proper SQL database-backed user system suitable for production applications. Source code and free course. https://industry-python.thinkific.com/ Technologies: Python FastAPI Shiny for Python HTTPX Pillow (PIL) Session Cookies bcrypt Passlib Next lesson: Moving user authentication and password management into a SQL database. #python #fastapi #shiny #httpx #authentication #sessioncookies #webdevelopment #backenddevelopment #pythonprogramming