ASGI#
To use the django_user_trace.asgi.UserTraceMiddleware
middleware, wrap it around the appropriate level of consumer in your asgi.py
alongside the AuthMiddlewareStack
:
from asgi_correlation_id.middleware import CorrelationIdMiddleware
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import re_path
from django_user_trace.asgi import UserTraceMiddleware
application = ProtocolTypeRouter(
{
"http": CorrelationIdMiddleware(#(2)!
AuthMiddlewareStack(
UserTraceMiddleware(#(1)!
URLRouter(
[
re_path(r"^foo", FooHttpConsumer.as_asgi()),#(3)!
]
)
)
)
),
"websocket": CorrelationIdMiddleware(
AuthMiddlewareStack(
UserTraceMiddleware(
URLRouter(
[
re_path(r"^bar", BarWebSocketConsumer.as_asgi()),
]
)
)
)
)
},
)
django-user-trace
uses middleware to capture user attributes from the ASGI scope'suser
key. To do this, the middleware must be placed withinAuthMiddlewareStack
to ensureuser
exists in the scope.- This is optional to show snok/asgi-correlation-id's integration.
- This is an example of an ASGI-only route — for example, you could register a Strawberry GraphQL consumer here.