Skip to content

Commit aad6915

Browse files
authored
Refetch tokens when expired (#235)
1 parent 568705f commit aad6915

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

app/components/layout.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ import useConnectionDetails from '@/hooks/useConnectionDetails';
1010
import { cn } from '@/lib/utils';
1111

1212
export default function ComponentsLayout({ children }: { children: React.ReactNode }) {
13-
const { connectionDetails } = useConnectionDetails();
13+
const { fetchConnectionDetails } = useConnectionDetails();
1414

1515
const pathname = usePathname();
1616
const room = React.useMemo(() => new Room(), []);
1717

1818
React.useEffect(() => {
19-
if (room.state === 'disconnected' && connectionDetails) {
19+
if (room.state === 'disconnected') {
2020
Promise.all([
2121
room.localParticipant.setMicrophoneEnabled(true, undefined, {
2222
preConnectBuffer: true,
2323
}),
24-
room.connect(connectionDetails.serverUrl, connectionDetails.participantToken),
24+
fetchConnectionDetails().then((connectionDetails) =>
25+
room.connect(connectionDetails.serverUrl, connectionDetails.participantToken)
26+
),
2527
]).catch((error) => {
2628
toastAlert({
2729
title: 'There was an error connecting to the agent',
@@ -32,7 +34,7 @@ export default function ComponentsLayout({ children }: { children: React.ReactNo
3234
return () => {
3335
room.disconnect();
3436
};
35-
}, [room, connectionDetails]);
37+
}, [room, fetchConnectionDetails]);
3638

3739
return (
3840
<div className="mx-auto min-h-svh max-w-3xl space-y-8 px-4 py-8">

components/app.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ interface AppProps {
2121
export function App({ appConfig }: AppProps) {
2222
const room = useMemo(() => new Room(), []);
2323
const [sessionStarted, setSessionStarted] = useState(false);
24-
const { connectionDetails, refreshConnectionDetails } = useConnectionDetails();
24+
const { fetchConnectionDetails } = useConnectionDetails();
2525

2626
useEffect(() => {
2727
const onDisconnected = () => {
2828
setSessionStarted(false);
29-
refreshConnectionDetails();
3029
};
3130
const onMediaDevicesError = (error: Error) => {
3231
toastAlert({
@@ -40,16 +39,18 @@ export function App({ appConfig }: AppProps) {
4039
room.off(RoomEvent.Disconnected, onDisconnected);
4140
room.off(RoomEvent.MediaDevicesError, onMediaDevicesError);
4241
};
43-
}, [room, refreshConnectionDetails]);
42+
}, [room]);
4443

4544
useEffect(() => {
4645
let aborted = false;
47-
if (sessionStarted && room.state === 'disconnected' && connectionDetails) {
46+
if (sessionStarted && room.state === 'disconnected') {
4847
Promise.all([
4948
room.localParticipant.setMicrophoneEnabled(true, undefined, {
5049
preConnectBuffer: appConfig.isPreConnectBufferEnabled,
5150
}),
52-
room.connect(connectionDetails.serverUrl, connectionDetails.participantToken),
51+
fetchConnectionDetails().then((connectionDetails) =>
52+
room.connect(connectionDetails.serverUrl, connectionDetails.participantToken)
53+
),
5354
]).catch((error) => {
5455
if (aborted) {
5556
// Once the effect has cleaned up after itself, drop any errors
@@ -70,7 +71,7 @@ export function App({ appConfig }: AppProps) {
7071
aborted = true;
7172
room.disconnect();
7273
};
73-
}, [room, sessionStarted, connectionDetails, appConfig.isPreConnectBufferEnabled]);
74+
}, [room, sessionStarted, fetchConnectionDetails, appConfig.isPreConnectBufferEnabled]);
7475

7576
const { startButtonText } = appConfig;
7677

hooks/useConnectionDetails.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useState } from 'react';
1+
import { useCallback } from 'react';
22
import { ConnectionDetails } from '@/app/api/connection-details/route';
33

44
export default function useConnectionDetails() {
@@ -11,27 +11,23 @@ export default function useConnectionDetails() {
1111
// In real-world application, you would likely allow the user to specify their
1212
// own participant name, and possibly to choose from existing rooms to join.
1313

14-
const [connectionDetails, setConnectionDetails] = useState<ConnectionDetails | null>(null);
15-
16-
const fetchConnectionDetails = useCallback(() => {
17-
setConnectionDetails(null);
14+
const fetchConnectionDetails = useCallback(async () => {
1815
const url = new URL(
1916
process.env.NEXT_PUBLIC_CONN_DETAILS_ENDPOINT ?? '/api/connection-details',
2017
window.location.origin
2118
);
22-
fetch(url.toString())
23-
.then((res) => res.json())
24-
.then((data) => {
25-
setConnectionDetails(data);
26-
})
27-
.catch((error) => {
28-
console.error('Error fetching connection details:', error);
29-
});
30-
}, []);
3119

32-
useEffect(() => {
33-
fetchConnectionDetails();
34-
}, [fetchConnectionDetails]);
20+
let data: ConnectionDetails;
21+
try {
22+
const res = await fetch(url.toString());
23+
data = await res.json();
24+
} catch (error) {
25+
console.error('Error fetching connection details:', error);
26+
throw new Error('Error fetching connection details!');
27+
}
28+
29+
return data;
30+
}, []);
3531

36-
return { connectionDetails, refreshConnectionDetails: fetchConnectionDetails };
32+
return { fetchConnectionDetails };
3733
}

0 commit comments

Comments
 (0)