Is there a way to create a custom `CoroutineContex...
# ktor
s
Is there a way to create a custom
CoroutineContext
that’s used for all requests? I swear there was a way to do this that I read a while ago, but I can’t find it in the documentation. If I use an Authentication provider, currently that authentication information is only stored inside the
call
. I’d like to create a custom
CoroutineContext
for each request, so that anywhere in the server that I’m in a
suspend
function, I can get the userId, instead of having to pass it through to every method call in my server.
After some more reading - going to try to implement it like this. Can anyone from the ktor team confirm this is the right way?
Copy code
intercept(ApplicationCallPipeline.Call) {
        withContext(coroutineContext + MyCoroutineContextElement(call)) {
            proceed()
        }
    }
r
This should work, but you may want to intercept the pipeline before
Call
phase to make context available in routing. Use
pipeline.insertPhaseBefore(Call, MyPhase)
for that