@@ -24,22 +24,15 @@ import {
2424import { createTextComposerEmojiMiddleware , EmojiPicker } from 'stream-chat-react/emojis' ;
2525import { init , SearchIndex } from 'emoji-mart' ;
2626import data from '@emoji-mart/data' ;
27+ import { humanId } from 'human-id' ;
2728
2829init ( { data } ) ;
2930
30- const params = new Proxy ( new URLSearchParams ( window . location . search ) , {
31- get : ( searchParams , property ) => searchParams . get ( property as string ) ,
32- } ) as unknown as Record < string , string | null > ;
31+ const apiKey = import . meta. env . VITE_STREAM_API_KEY ;
3332
34- const parseUserIdFromToken = ( token : string ) => {
35- const [ , payload ] = token . split ( '.' ) ;
36-
37- if ( ! payload ) throw new Error ( 'Token is missing' ) ;
38-
39- return JSON . parse ( atob ( payload ) ) ?. user_id ;
40- } ;
41-
42- const apiKey = params . key ?? ( import . meta. env . VITE_STREAM_API_KEY as string ) ;
33+ if ( ! apiKey ) {
34+ throw new Error ( 'VITE_STREAM_API_KEY is not defined' ) ;
35+ }
4336
4437const options : ChannelOptions = { limit : 5 , presence : true , state : true } ;
4538const sort : ChannelSort = { pinned_at : 1 , last_message_at : - 1 , updated_at : - 1 } ;
@@ -50,22 +43,24 @@ const isMessageAIGenerated = (message: LocalMessage) => !!message?.ai_generated;
5043const useUser = ( ) => {
5144 const userId = useMemo ( ( ) => {
5245 return (
53- new URLSearchParams ( window . location . search ) . get ( 'userId ' ) ||
54- localStorage . getItem ( 'userId ' ) ||
55- `user- ${ crypto . randomUUID ( ) . slice ( 0 , 8 ) } `
46+ new URLSearchParams ( window . location . search ) . get ( 'user_id ' ) ||
47+ localStorage . getItem ( 'user_id ' ) ||
48+ humanId ( { separator : '_' , capitalize : false } )
5649 ) ;
5750 } , [ ] ) ;
5851
5952 useEffect ( ( ) => {
60- const storedUserId = localStorage . getItem ( 'userId ' ) ;
53+ const storedUserId = localStorage . getItem ( 'user_id ' ) ;
6154
6255 if ( userId && storedUserId === userId ) return ;
6356
64- localStorage . setItem ( 'userId ' , userId ) ;
57+ localStorage . setItem ( 'user_id ' , userId ) ;
6558 } , [ userId ] ) ;
6659
6760 const tokenProvider = useCallback ( ( ) => {
68- return fetch ( `https://pronto.getstream.io/api/auth/create-token?user_id=${ userId } ` )
61+ return fetch (
62+ `https://pronto.getstream.io/api/auth/create-token?environment=shared-chat-redesign&user_id=${ userId } ` ,
63+ )
6964 . then ( ( response ) => response . json ( ) )
7065 . then ( ( data ) => data . token as string ) ;
7166 } , [ userId ] ) ;
0 commit comments