A session on flask.g
authdog.session() resolves the request's session and caches it on flask.g, returning a typed AuthdogContext with token, user, and is_authenticated from any view.
- Typed AuthdogContext
- Cached on flask.g
Python · Flask
A session resolver, a require_auth decorator, and a logout handler for Flask. The context resolves once and caches on flask.g. Validate Authdog sessions on every request, on the same wire as the Node SDKs.
Wire up the app
# app.py
from flask import Flask
from authdog.flask import Authdog
app = Flask(__name__)
authdog = Authdog(public_key=os.environ["PK_AUTHDOG"])
@app.get("/")
def index():
return {"authenticated": authdog.session().is_authenticated}
Gate views with a decorator
# app.py
@app.get("/me")
@authdog.require_auth
def me():
return authdog.session().user
Decorators and flask.g
A drop-in session resolver, a require_auth decorator, and a typed context cached on flask.g.
authdog.session() resolves the request's session and caches it on flask.g, returning a typed AuthdogContext with token, user, and is_authenticated from any view.
Stack @authdog.require_auth on a view and Flask rejects unauthenticated requests with a 401, returning the user object otherwise. This is the security boundary.
The resolved context is cached on flask.g, so calling session() several times in one request still makes a single outbound userinfo call.
The bindings are ordinary view decorators. Stack them under your route, combine them with blueprints, and keep your app's structure unchanged.
authdog.logout() returns a redirect Response that expires the session cookie and redirects to a redirect_uri sanitized against open redirects.
It mirrors @authdog/express and @authdog/fastify on the wire, so one Authdog environment serves your Node and Flask services interchangeably.
pip install 'authdog-fastapi[flask]'.
Instantiate Authdog with your environment's public key.
Stack require_auth under the routes you protect.
pip install 'authdog-fastapi[flask]', add the decorator, and gate your views with require_auth today. Free to start, with secure defaults built in.