-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(react-native): get apikey from endpoint
- Loading branch information
1 parent
bfeba2a
commit 4a75763
Showing
16 changed files
with
142 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 27 additions & 32 deletions
59
sample-apps/react-native/dogfood/src/hooks/useChatClient.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,59 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { | ||
StreamChat, | ||
OwnUserResponse, | ||
UserResponse, | ||
TokenOrProvider, | ||
} from 'stream-chat'; | ||
import { StreamChat, OwnUserResponse, UserResponse } from 'stream-chat'; | ||
import { StreamChatGenerics } from '../../types'; | ||
import { createToken } from '../modules/helpers/createToken'; | ||
|
||
export const useChatClient = < | ||
SCG extends StreamChatGenerics = StreamChatGenerics, | ||
>({ | ||
apiKey, | ||
userData, | ||
tokenProvider, | ||
}: { | ||
apiKey: string; | ||
userData?: OwnUserResponse<SCG> | UserResponse<SCG>; | ||
tokenProvider?: TokenOrProvider; | ||
userData: OwnUserResponse<SCG> | UserResponse<SCG>; | ||
}) => { | ||
const [chatClient, setChatClient] = useState<StreamChat<SCG> | null>(null); | ||
const [chatClient, setChatClient] = useState<StreamChat<SCG> | undefined>(); | ||
const disconnectRef = useRef(Promise.resolve()); | ||
|
||
useEffect(() => { | ||
if (!userData) { | ||
return; | ||
} | ||
|
||
const client = new StreamChat<SCG>(apiKey); | ||
const connectUser = async () => { | ||
await disconnectRef.current; | ||
try { | ||
await client.connectUser(userData, tokenProvider); | ||
console.log(`[Chat client]: Connected user: ${userData.id}`); | ||
} catch (e) { | ||
console.error('[Chat client]: Failed to establish connection', e); | ||
} | ||
if (!didUserConnectInterrupt) { | ||
setChatClient(client); | ||
} | ||
let connectPromise: Promise<void> | undefined; | ||
let client: StreamChat<SCG> | undefined; | ||
const run = async () => { | ||
const { token, apiKey } = await createToken({ user_id: userData.id }); | ||
client = new StreamChat<SCG>(apiKey); | ||
const connectUser = async () => { | ||
await disconnectRef.current; | ||
try { | ||
await client?.connectUser(userData, token); | ||
console.log(`[Chat client]: Connected user: ${userData.id}`); | ||
} catch (e) { | ||
console.error('[Chat client]: Failed to establish connection', e); | ||
} | ||
if (!didUserConnectInterrupt) { | ||
setChatClient(client); | ||
} | ||
}; | ||
connectPromise = connectUser(); | ||
}; | ||
|
||
run(); | ||
|
||
let didUserConnectInterrupt = false; | ||
const connectPromise = connectUser(); | ||
|
||
const cleanUp = async () => { | ||
didUserConnectInterrupt = true; | ||
await connectPromise; | ||
try { | ||
await client.disconnectUser(); | ||
await client?.disconnectUser(); | ||
console.log(`[Chat client]: Disconnected user: ${userData.id}`); | ||
} catch (e) { | ||
console.error('[Chat client]: Failed to disconnect', e); | ||
} | ||
setChatClient(null); | ||
setChatClient(undefined); | ||
}; | ||
|
||
return () => { | ||
disconnectRef.current = cleanUp(); | ||
}; | ||
}, [apiKey, userData, tokenProvider]); | ||
}, [userData]); | ||
|
||
return chatClient; | ||
}; |
20 changes: 12 additions & 8 deletions
20
sample-apps/react-native/dogfood/src/modules/helpers/createToken.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
import { STREAM_API_KEY } from '../../../config'; | ||
|
||
type ParamsType = { | ||
user_id: string; | ||
call_cids?: string; | ||
}; | ||
|
||
export const createToken = async (params: ParamsType) => { | ||
const endpoint = new URL( | ||
'https://stream-calls-dogfood.vercel.app/api/auth/create-token', | ||
); | ||
endpoint.searchParams.set('api_key', STREAM_API_KEY); | ||
type EnvironmentType = 'pronto' | 'demo'; | ||
|
||
export const createToken = async ( | ||
params: ParamsType, | ||
environment: EnvironmentType = 'pronto', | ||
) => { | ||
const endpoint = new URL('https://pronto.getstream.io/api/auth/create-token'); | ||
endpoint.searchParams.set('user_id', params.user_id); | ||
endpoint.searchParams.set('environment', environment); | ||
endpoint.searchParams.set('exp', String(4 * 60 * 60)); // 4 hours | ||
if (params.call_cids) { | ||
endpoint.searchParams.set('call_cids', params.call_cids); | ||
} | ||
const response = await fetch(endpoint.toString()).then((res) => res.json()); | ||
return response.token as string; | ||
return { | ||
token: response.token as string, | ||
apiKey: response.apiKey as string, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.