2024-03-15 22:48:28 +01:00
|
|
|
import { createClient } from "@supabase/supabase-js"
|
2024-03-22 23:41:51 +01:00
|
|
|
import { AppState, Platform } from "react-native"
|
|
|
|
import "react-native-url-polyfill/auto"
|
|
|
|
import AsyncStorage from "@react-native-async-storage/async-storage"
|
2024-03-15 22:48:28 +01:00
|
|
|
|
|
|
|
import type { Database } from "./supabase-types"
|
|
|
|
|
|
|
|
const SUPABASE_URL = process.env["EXPO_PUBLIC_SUPABASE_URL"] ?? ""
|
|
|
|
const SUPABASE_ANON_KEY = process.env["EXPO_PUBLIC_SUPABASE_ANON_KEY"] ?? ""
|
|
|
|
|
|
|
|
export const supabaseClient = createClient<Database>(
|
|
|
|
SUPABASE_URL,
|
|
|
|
SUPABASE_ANON_KEY,
|
2024-03-22 23:41:51 +01:00
|
|
|
{
|
|
|
|
auth: {
|
|
|
|
// https://github.com/supabase/supabase-js/issues/870
|
|
|
|
...(Platform.OS !== "web" ? { storage: AsyncStorage } : {}),
|
|
|
|
autoRefreshToken: true,
|
|
|
|
persistSession: true,
|
|
|
|
detectSessionInUrl: false,
|
|
|
|
},
|
|
|
|
},
|
2024-03-15 22:48:28 +01:00
|
|
|
)
|
2024-03-22 23:41:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells Supabase Auth to continuously refresh the session automatically if the app is in the foreground.
|
|
|
|
* When this is added, you will continue to receive `onAuthStateChange` events with the `TOKEN_REFRESH` or `SIGNED_OUT` event if the user's session is terminated.
|
|
|
|
* This should only be registered once.
|
|
|
|
*/
|
|
|
|
AppState.addEventListener("change", async (state) => {
|
|
|
|
if (state === "active") {
|
|
|
|
await supabaseClient.auth.startAutoRefresh()
|
|
|
|
} else {
|
|
|
|
await supabaseClient.auth.stopAutoRefresh()
|
|
|
|
}
|
|
|
|
})
|