Skip to content

Commit ee43c62

Browse files
committed
fix: refresh token on settings updated
1 parent cf6bc24 commit ee43c62

File tree

7 files changed

+252
-166
lines changed

7 files changed

+252
-166
lines changed

app/(home)/index.tsx

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,48 @@
11
import Auth, { fetchUserSettings, sleep } from "@/components/screens/auth";
22
import { HomeScreen } from "@/components/screens/home";
33
import { SignupFlow } from "@/components/screens/signup";
4+
import { SessionContext } from "@/context/SessionContext"; // Import your SessionContext
45
import { segmentTrackOpened } from "@/lib/analytics";
5-
import { supabase } from "@/lib/supabase";
66
import { Settings } from "@prisma/client";
77
import { Session } from "@supabase/supabase-js";
8-
import { useEffect, useState } from "react";
8+
import React, { useContext, useEffect, useState } from "react";
99
import { ActivityIndicator, StyleSheet, View } from "react-native";
10-
import "react-native-url-polyfill/auto";
1110

1211
export default function App() {
13-
const [session, setSession] = useState<Session | null>(null);
12+
const { session, loading: sessionLoading } = useContext(SessionContext);
1413
const [showSignupFlow, setShowSignupFlow] = useState<boolean>(false);
1514
const [settings, setSettings] = useState<Settings | null>();
1615
const [loading, setLoading] = useState<boolean>(true);
1716

1817
const setupSettings = async (userId: string) => {
1918
setSettings(await fetchUserSettings(userId));
20-
sleep(1000);
19+
await sleep(1000);
2120
setLoading(false);
2221
};
2322

24-
const handleSetupSettings = async (session: Session | null) => {
25-
if (session) {
26-
segmentTrackOpened(session.user.id);
23+
const handleSetupSettings = async (currentSession: Session | null) => {
24+
if (currentSession) {
25+
segmentTrackOpened(currentSession.user.id);
2726
setLoading(true);
28-
setupSettings(session.user.id);
27+
await setupSettings(currentSession.user.id);
2928
} else {
3029
setSettings(null);
30+
setLoading(false);
3131
}
3232
};
3333

3434
useEffect(() => {
3535
handleSetupSettings(session);
3636
}, [session]);
3737

38-
useEffect(() => {
39-
supabase.auth.getSession().then(({ data: { session } }) => {
40-
setSession(session);
41-
});
42-
43-
supabase.auth.onAuthStateChange(async (_event, session) => {
44-
setSession(session);
45-
});
46-
}, []);
47-
4838
const didConfigureSettings =
4939
settings &&
50-
settings?.agentName !== null &&
51-
settings?.gender !== null &&
40+
settings.agentName !== null &&
41+
settings.gender !== null &&
5242
settings.name !== null &&
5343
settings.voice !== null;
5444

55-
if (session && loading) {
45+
if (sessionLoading || (session && loading)) {
5646
return (
5747
<View style={styles.container}>
5848
<ActivityIndicator />
@@ -63,10 +53,10 @@ export default function App() {
6353
if (!session) {
6454
return (
6555
<View style={styles.container}>
66-
<Auth setSession={setSession} setShowSignupFlow={setShowSignupFlow} />
56+
<Auth setShowSignupFlow={setShowSignupFlow} />
6757
</View>
6858
);
69-
} else if (showSignupFlow || didConfigureSettings === false) {
59+
} else if (showSignupFlow || !didConfigureSettings) {
7060
return (
7161
<View style={styles.container}>
7262
<SignupFlow

app/_layout.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
// app/_layout.tsx
2-
import React from 'react';
3-
import { Stack } from 'expo-router';
4-
import { registerGlobals } from '@livekit/react-native';
5-
import * as Sentry from '@sentry/react-native';
2+
3+
import { SessionProvider } from "@/context/SessionContext";
4+
import { registerGlobals } from "@livekit/react-native";
65
import { captureConsoleIntegration } from "@sentry/integrations";
6+
import * as Sentry from "@sentry/react-native";
7+
import { Stack } from "expo-router";
8+
import React from "react";
9+
import { AppContextProvider } from "../context";
710

811
Sentry.init({
912
dsn: process.env.EXPO_PUBLIC_SENTRY_DSN,
1013
environment: process.env.EXPO_PUBLIC_SENTRY_ENV,
11-
integrations: [
12-
captureConsoleIntegration({ levels: ["warn", "error"] }),
13-
]
14-
14+
integrations: [captureConsoleIntegration({ levels: ["warn", "error"] })],
1515
// uncomment the line below to enable Spotlight (https://spotlightjs.com)
1616
// enableSpotlight: __DEV__,
1717
});
1818

19-
registerGlobals()
19+
registerGlobals();
2020

2121
const RootLayout: React.FC = () => {
2222
return (
23-
<Stack screenOptions={{ headerShown: false }}>
24-
<Stack.Screen name="(home)" options={{ headerShown: false }} />
25-
<Stack.Screen name="settings" options={{ title: 'Settings' }} />
26-
</Stack>
23+
<SessionProvider>
24+
<AppContextProvider>
25+
<Stack screenOptions={{ headerShown: false }}>
26+
<Stack.Screen name="(home)" options={{ headerShown: false }} />
27+
<Stack.Screen name="settings" options={{ title: "Settings" }} />
28+
</Stack>
29+
</AppContextProvider>
30+
</SessionProvider>
2731
);
2832
};
2933

components/chat-interfaces/voice/index.tsx

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@ import {
55
registerGlobals,
66
useRemoteParticipant,
77
} from "@livekit/react-native";
8+
import { useFocusEffect } from "@react-navigation/native";
89
import { Session } from "@supabase/supabase-js";
910
import useAxios, { RefetchFunction } from "axios-hooks";
1011
import { ParticipantKind } from "livekit-client";
11-
import * as React from "react";
12-
import { useEffect } from "react";
12+
import React, { useEffect } from "react";
1313
import { ActivityIndicator, Alert, Button, View } from "react-native";
1414

1515
registerGlobals();
1616

17-
const RoomConnecting = () => {
18-
return <ActivityIndicator />;
19-
};
20-
2117
const RoomStatus = ({
2218
connected,
2319
setConnected,
@@ -36,38 +32,29 @@ const RoomStatus = ({
3632
const agentConnected = !!remote;
3733

3834
useEffect(() => {
39-
// If call connected and agent connected and first time this is true, then record that the agent connected at least once
4035
if (connected && agentConnected && !agentPreviouslyConnected) {
4136
setAgentPreviouslyConnected(true);
4237
}
4338

44-
// If the agent was previously connected and the call is connected, but there is no longer an agent connected then trigger
45-
// the room to be destroyed and recreated (which will cause a new agent to be connected)
4639
if (agentPreviouslyConnected && connected && !agentConnected) {
4740
refetchToken();
4841
}
4942
}, [agentConnected, connected]);
5043

5144
return (
52-
<>
53-
<View
54-
style={{
55-
marginBottom: 0,
56-
}}
57-
>
58-
{connected && agentConnected === false ? (
59-
<ActivityIndicator />
60-
) : (
61-
<Button
62-
title={"End Chat"}
63-
onPress={() => {
64-
setConnected(!connected);
65-
segmentTrackEndChat();
66-
}}
67-
/>
68-
)}
69-
</View>
70-
</>
45+
<View style={{ marginBottom: 0 }}>
46+
{connected && agentConnected === false ? (
47+
<ActivityIndicator />
48+
) : (
49+
<Button
50+
title={"End Chat"}
51+
onPress={() => {
52+
setConnected(false);
53+
segmentTrackEndChat();
54+
}}
55+
/>
56+
)}
57+
</View>
7158
);
7259
};
7360

@@ -86,6 +73,16 @@ export const VoiceInterface: React.FC<{ session: Session }> = ({ session }) => {
8673
},
8774
});
8875

76+
useFocusEffect(
77+
React.useCallback(() => {
78+
return () => {
79+
if (connected) {
80+
setConnected(false);
81+
}
82+
};
83+
}, [connected])
84+
);
85+
8986
useEffect(() => {
9087
if (error !== null) {
9188
console.error(error);
@@ -101,18 +98,19 @@ export const VoiceInterface: React.FC<{ session: Session }> = ({ session }) => {
10198
return;
10299
}
103100

104-
let start = async () => {
101+
const startAudioSession = async () => {
105102
await AudioSession.startAudioSession();
106103
};
107104

108-
start();
105+
startAudioSession();
106+
109107
return () => {
110108
AudioSession.stopAudioSession();
111109
};
112110
}, [loading]);
113111

114112
if (loading || token === null) {
115-
return <></>;
113+
return null;
116114
}
117115

118116
return (
@@ -121,7 +119,7 @@ export const VoiceInterface: React.FC<{ session: Session }> = ({ session }) => {
121119
<Button
122120
title={"Start Chat"}
123121
onPress={() => {
124-
setConnected(!connected);
122+
setConnected(true);
125123
segmentTrackStartChat();
126124
}}
127125
/>
@@ -130,7 +128,7 @@ export const VoiceInterface: React.FC<{ session: Session }> = ({ session }) => {
130128
<LiveKitRoom
131129
serverUrl={wsURL}
132130
token={token}
133-
connect={token && connected}
131+
connect={connected}
134132
audio={true}
135133
>
136134
<RoomStatus

components/screens/auth/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SignInWithApple } from "@/components/screens/auth/apple";
22
import { VoiceKey } from "@/components/screens/signup/voice";
3+
import { SessionContext } from "@/context/SessionContext";
34
import {
45
segmentTrackLoadedAuthPage,
56
segmentTrackSignedIn,
@@ -9,12 +10,11 @@ import { convertSQLToSettings } from "@/lib/utils";
910
import { Settings } from "@prisma/client";
1011
import { GoogleSignin } from "@react-native-google-signin/google-signin";
1112
import { Session, User } from "@supabase/supabase-js";
12-
import React, { useEffect } from "react";
13+
import React, { useContext, useEffect } from "react";
1314
import { StyleSheet, Text, View } from "react-native";
1415
import { SignInWithGoogle } from "./google";
1516

1617
type Props = {
17-
setSession: React.Dispatch<React.SetStateAction<Session | null>>;
1818
setShowSignupFlow: React.Dispatch<React.SetStateAction<boolean>>;
1919
};
2020

@@ -50,12 +50,14 @@ export const updateUserSettings = async (
5050
export const sleep = (ms: number) =>
5151
new Promise((resolve) => setTimeout(resolve, ms));
5252

53-
export default function Auth({ setSession, setShowSignupFlow }: Props) {
53+
export default function Auth({ setShowSignupFlow }: Props) {
5454
GoogleSignin.configure({
5555
scopes: [],
5656
iosClientId: process.env.EXPO_PUBLIC_GOOGLE_AUTH_CLIENT_ID,
5757
});
5858

59+
const { setSession } = useContext(SessionContext);
60+
5961
const handleDidSignin = async (user: User, session: Session) => {
6062
setSession(session);
6163
let settings = null;

0 commit comments

Comments
 (0)