fix: Allow guest and anonymous users without auth options#2140
fix: Allow guest and anonymous users without auth options#2140
Conversation
|
📝 WalkthroughWalkthroughSplit user shapes and client option variants: added Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
sample-apps/react/livestream-app/src/hooks/useInitVideoClient.ts (1)
31-55: TokenProvider is created but unused for anonymous/guest users.The
tokenProviderfunction (lines 31-47) is created unconditionally, but for anonymous/guest users (lines 48-50), it's never used. While this doesn't cause functional issues, it's slightly wasteful to define the function.Consider moving the tokenProvider definition inside the else branch:
♻️ Optional: move tokenProvider inside else branch
- const tokenProvider = async () => { - const endpoint = new URL(tokenProviderUrl); - endpoint.searchParams.set('api_key', apiKey); - endpoint.searchParams.set( - 'user_id', - user.type === 'anonymous' ? '!anon' : user.id!, - ); - - if (user.type === 'anonymous') { - endpoint.searchParams.set( - 'call_cids', - `${type ?? DEFAULT_CALL_TYPE}:${callId}`, - ); - } - const response = await fetch(endpoint).then((res) => res.json()); - return response.token as string; - }; const _client = user.type === 'anonymous' || user.type === 'guest' ? new StreamVideoClient({ apiKey, user }) - : new StreamVideoClient({ + : (() => { + const tokenProvider = async () => { + const endpoint = new URL(tokenProviderUrl); + endpoint.searchParams.set('api_key', apiKey); + endpoint.searchParams.set('user_id', user.id!); + const response = await fetch(endpoint).then((res) => res.json()); + return response.token as string; + }; + return new StreamVideoClient({ apiKey, user, ...(token ? { token, tokenProvider } : { tokenProvider }), - }); + }); + })();🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@sample-apps/react/livestream-app/src/hooks/useInitVideoClient.ts` around lines 31 - 55, The tokenProvider function is created unconditionally but only used when user.type is neither 'anonymous' nor 'guest'; move the tokenProvider definition inside the else branch that constructs the StreamVideoClient for authenticated users so it’s only created when needed, and ensure the StreamVideoClient construction still passes either { token, tokenProvider } when token is present or { tokenProvider } otherwise; update references to tokenProvider and StreamVideoClient in that block and remove the now-unused top-level tokenProvider.sample-apps/react-native/expo-video-sample/components/VideoWrapper.tsx (1)
21-31: Unnecessary token fetch for guest/anonymous users.The
fetchAuthDetails()call on line 24 is executed unconditionally, but for guest/anonymous users (lines 26-31), the fetchedtokenis never used. This results in a wasted network request.Consider moving the fetch inside the else branch or conditionally fetching only for authenticated users:
♻️ Proposed refactor to avoid unnecessary fetch
if (!user) return; const user_id = user.type === 'anonymous' ? '!anon' : user.id; if (!user_id) return; - const fetchAuthDetails = async () => { - return await createToken({ user_id }); - }; - const { apiKey, token } = await fetchAuthDetails(); if (user.type === 'guest' || user.type === 'anonymous') { + const { apiKey } = await createToken({ user_id }); _videoClient = StreamVideoClient.getOrCreateInstance({ apiKey, user, options: { logLevel: 'warn' }, }); } else { + const fetchAuthDetails = async () => { + return await createToken({ user_id }); + }; + const { apiKey, token } = await fetchAuthDetails(); const tokenProvider = () => fetchAuthDetails().then((auth) => auth.token);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@sample-apps/react-native/expo-video-sample/components/VideoWrapper.tsx` around lines 21 - 31, The code currently calls fetchAuthDetails()/createToken unconditionally but never uses the returned token for guest/anonymous users; update the logic so you only call fetchAuthDetails() when user.type is not 'guest' or 'anonymous' (i.e., move the await createToken({ user_id }) / const { apiKey, token } into the else branch), or alternatively fetch only the apiKey for guests if needed; adjust usage around apiKey/token and StreamVideoClient.getOrCreateInstance so no network call is made for guest/anonymous users.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@sample-apps/react/livestream-app/src/hooks/useInitVideoClient.ts`:
- Around line 34-37: In useInitVideoClient.ts update the
endpoint.searchParams.set call to avoid the non-null assertion on user.id by
adding a runtime guard: when setting 'user_id' use user.type === 'anonymous' ?
'!anon' : (user.id ?? '<fallback>') (or choose a sensible fallback like
'!unknown' and/or log an error via the same logger used elsewhere) rather than
user.id!; ensure you reference getUser()’s returned object and handle both
missing id and guest/anonymous types so no non-null assertion is used.
---
Nitpick comments:
In `@sample-apps/react-native/expo-video-sample/components/VideoWrapper.tsx`:
- Around line 21-31: The code currently calls fetchAuthDetails()/createToken
unconditionally but never uses the returned token for guest/anonymous users;
update the logic so you only call fetchAuthDetails() when user.type is not
'guest' or 'anonymous' (i.e., move the await createToken({ user_id }) / const {
apiKey, token } into the else branch), or alternatively fetch only the apiKey
for guests if needed; adjust usage around apiKey/token and
StreamVideoClient.getOrCreateInstance so no network call is made for
guest/anonymous users.
In `@sample-apps/react/livestream-app/src/hooks/useInitVideoClient.ts`:
- Around line 31-55: The tokenProvider function is created unconditionally but
only used when user.type is neither 'anonymous' nor 'guest'; move the
tokenProvider definition inside the else branch that constructs the
StreamVideoClient for authenticated users so it’s only created when needed, and
ensure the StreamVideoClient construction still passes either { token,
tokenProvider } when token is present or { tokenProvider } otherwise; update
references to tokenProvider and StreamVideoClient in that block and remove the
now-unused top-level tokenProvider.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (4)
sample-apps/react-native/dogfood/src/screens/Meeting/GuestMeetingScreen.tsxsample-apps/react-native/expo-video-sample/components/VideoWrapper.tsxsample-apps/react/livestream-app/src/hooks/useInitVideoClient.tssample-apps/react/react-dogfood/helpers/client.ts
💡 Overview
Update StreamVideoClientOptions typing so guest and anonymous users must not provide token or tokenProvider, while authenticated users must provide at least one of them.
Continuation of: #2138
📝 Implementation notes
🎫 Ticket: https://linear.app/stream/issue/RN-352/error-in-streamvideoclientgetorcreateinstance-method-and-user
📑 Docs: https://github.com/GetStream/docs-content/pull/1042
Summary by CodeRabbit
New Features
Bug Fixes