Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions examples/vite/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 }),
Expand Down