cha0sg0dC
tRPC2y ago
6 replies
cha0sg0d

Efficient way to use tRPC client with auth headers from secure storage

Wondering if anyone has a recommended pattern on caching the deviceId / authHeader using a React Context instead of fetching it async on each request.

Thanks!

export const trpc = createTRPCClient<AppRouter>({
  links: [
    httpBatchLink({
      url: getServerUrl(),
      async headers() {
        const authorization = await getAuthHeader()
        const deviceId = await createOrGetDeviceIdFromStore()
        return {
          authorization,
          'x-device-id': deviceId,
        }
      }
    })
  ]
})
Was this page helpful?