From 2bbdc15bb16ff7aac9bb0f7db9173429d4147d36 Mon Sep 17 00:00:00 2001 From: Oliver Lazoroski Date: Fri, 5 Jun 2026 16:04:17 +0200 Subject: [PATCH] chore(demo): support api_key query param in vite example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Read the Stream API key from an `api_key` query param (falling back to VITE_STREAM_API_KEY), mirroring the existing `token` handling. Derive the user id from a provided `token` so `?api_key=…&token=…` works without a separate `user_id`, and parse tokens defensively so a malformed value falls through instead of throwing during render. --- examples/vite/src/App.tsx | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/examples/vite/src/App.tsx b/examples/vite/src/App.tsx index d48493fff..9ce215e2f 100644 --- a/examples/vite/src/App.tsx +++ b/examples/vite/src/App.tsx @@ -74,15 +74,21 @@ const PUBLIC_VITE_EXAMPLE_API_KEY = 'xzwhhgtazy6h'; init({ data }); -const parseUserIdFromToken = (token: string) => { - const [, payload] = token.split('.'); +const parseUserIdFromToken = (token: string): string | undefined => { + try { + const [, payload] = token.split('.'); - if (!payload) throw new Error('Token is missing'); + if (!payload) return undefined; - return JSON.parse(atob(payload))?.user_id; + return JSON.parse(atob(payload))?.user_id; + } catch { + return undefined; + } }; -const apiKey = import.meta.env.VITE_STREAM_API_KEY; +const apiKey = + new URLSearchParams(window.location.search).get('api_key') || + import.meta.env.VITE_STREAM_API_KEY; const token = new URLSearchParams(window.location.search).get('token') || import.meta.env.VITE_USER_TOKEN; @@ -107,6 +113,7 @@ const useUser = () => { const userId = useMemo( () => searchParams.get('user_id') || + (token ? parseUserIdFromToken(token) : undefined) || import.meta.env.VITE_USER_ID || localStorage.getItem('user_id') || humanId({ separator: '_', capitalize: false }),