Skip to content

Commit b7b019f

Browse files
committed
add fallback if supabase fails to initialize
1 parent 9f3e598 commit b7b019f

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

GamerMatchApp/app/(tabs)/profileScreen.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ console.log("Temp User Profile:",tempProfile);
292292
* stores the image in Supabase Storage and sends it to MongoDB.
293293
*/
294294
const handleProfilePictureEdit = async () => {
295+
if(!supabase){ // if supabase does not initialize properly, alert the user that they cannot upload an image at this time
296+
Alert.alert("Offline","Supabase did not initialize properly, cannot upload image. Please try again later.");
297+
return;
298+
}
295299
try {
296300
setUploading(true);
297301
const phoneNumber = await AsyncStorage.getItem("currentUserPhoneNumber");

GamerMatchApp/lib/supabase.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
// Connects Supabase to Expo React Native
22
import 'react-native-url-polyfill/auto'
3-
import { createClient } from '@supabase/supabase-js'
3+
import { createClient, SupabaseClient } from '@supabase/supabase-js'
44
import 'expo-sqlite/localStorage/install';
5+
import Constants from "expo-constants";
6+
import AsyncStorage from '@react-native-async-storage/async-storage';
57

8+
const extras = Constants.expoConfig?.extra ?? {};
69
const supabaseUrl = process.env.EXPO_PUBLIC_SUPABASE_URL
710
const supabaseAnonKey = process.env.EXPO_PUBLIC_SUPABASE_PUBLISHABLE_KEY
11+
// allow supabase to be null if env vars are missing
12+
export let supabase: SupabaseClient | null = null;
813

9-
if (!supabaseUrl || !supabaseAnonKey){
10-
throw new Error("Missing Supabase environment variables.")
11-
}
12-
13-
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
14-
auth: {
15-
storage: localStorage,
16-
autoRefreshToken: true,
17-
persistSession: true,
18-
detectSessionInUrl: false,
19-
},
20-
})
14+
if (supabaseUrl && supabaseAnonKey) {
15+
supabase = createClient(supabaseUrl, supabaseAnonKey, {
16+
auth: {
17+
storage: AsyncStorage,
18+
autoRefreshToken: true,
19+
persistSession: true,
20+
detectSessionInUrl: false,
21+
},
22+
});
23+
} else {
24+
// do not throw, allow app to run without Supabase for dev or missing config
25+
console.warn('[supabase] env vars missing. running without Supabase');
26+
}

0 commit comments

Comments
 (0)