Add Session Middleware
Integrate the required session middleware to manage and track authenticated user sessions.
Track user authentication status seamlessly by adding Wristband's session middleware to your project. This middleware attaches a request.state.session object containing user information to incoming requests. Session data remains protected within an encrypted cookie, which is automatically decrypted on subsequent requests to seamlessly rebuild the session state.
Configure Session Middleware
Activate session management by registering the Wristband SDK's SessionMiddleware inside your FastAPI application. You must supply an accompanying secret_key that is at least 32 characters long. A secure key can be generated instantly via https://securepassword.dev .
Disabling Secure Session Cookies in Local DevelopmentFor security, session cookies are flagged as
secureby default and require HTTPS. Most modern browsers grant a local testing exception forhttp://localhost, but Safari maintains higher security baselines and will refuse to transmit secure cookies over HTTP, even onlocalhost.If you need to turn off the secure flag for local development, configure
secure=Falsein yourSessionMiddleware. However, it is critical to restoresecure=Truein production to keep your user session data fully protected.
# src/main.py
from fastapi import FastAPI
from wristband.fastapi_auth import SessionMiddleware
def create_app() -> FastAPI:
app = FastAPI()
# Enable Wristband's session middlware on all routes.
app.add_middleware(SessionMiddleware, secret_key="<your-generated-secret>")
# Your other application setup...# src/main.py
from fastapi import FastAPI
from wristband.fastapi_auth import SessionMiddleware
def create_app() -> FastAPI:
app = FastAPI()
# Enable Wristband's session middlware on all routes.
app.add_middleware(SessionMiddleware, secret_key="<your-generated-secret>", secure=False)
# Your other application setup...Updated 11 days ago
What’s Next
Next, you'll use the Wristband SDK to create the necessary authentication endpoints in your FastAPI server.