shlogg · Early preview
Ramu Narasinga @karthik-m22

How TRPC Ctx Adds Logged-in UserId In LobeChat Source Code

LobeChat's tRPC ctx adds userId from Clerk auth, createContext returns object with context set, createContextInner function sets userId from params

In this article, we analyse how the currently logged-in userId is added to the tRPC ctx by reviewing LobeChat source code. I recommend reading this tRPC setup to relate to the concepts explained in this article.
But before that, I want to explain how I found this. I study LobeChat source code and was working on a project that involved tRPC + Supabase in a Next.js app router. I am following the similar codebase architecture to LobeChat’s.

LobeChat uses Clerk for Authentication, the feature I was working required auth wall before making a request to the backend. Since I am following the LobeCha...