This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
p61-project/infrastructure/supabase/supabase.ts

59 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2024-05-02 01:08:27 +02:00
import {
createClient,
type User as SupabaseUserType,
} from "@supabase/supabase-js"
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"
2024-05-02 01:08:27 +02:00
export type SupabaseUser = SupabaseUserType
2024-05-02 23:48:47 +02:00
2024-05-02 01:08:27 +02:00
export type SupabaseHabit = Database["public"]["Tables"]["habits"]["Row"]
2024-05-02 23:48:47 +02:00
export type SupabaseHabitInsert =
Database["public"]["Tables"]["habits"]["Insert"]
export type SupabaseHabitUpdate =
Database["public"]["Tables"]["habits"]["Update"]
2024-05-02 01:08:27 +02:00
export type SupabaseHabitProgress =
Database["public"]["Tables"]["habits_progresses"]["Row"]
2024-05-02 23:48:47 +02:00
export type SupabaseHabitProgressInsert =
Database["public"]["Tables"]["habits_progresses"]["Insert"]
export type SupabaseHabitProgressUpdate =
Database["public"]["Tables"]["habits_progresses"]["Update"]
2024-05-02 01:08:27 +02:00
2024-04-05 00:08:40 +02:00
const SUPABASE_URL =
process.env["EXPO_PUBLIC_SUPABASE_URL"] ??
"https://wjtwtzxreersqfvfgxrz.supabase.co"
const SUPABASE_ANON_KEY =
process.env["EXPO_PUBLIC_SUPABASE_ANON_KEY"] ??
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6IndqdHd0enhyZWVyc3FmdmZneHJ6Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTExNDcyNzAsImV4cCI6MjAyNjcyMzI3MH0.AglONhsMvmcCRkqwrZsB4Ws9u3o1FAbLlpKJmqeUv8I"
2024-03-15 22:48:28 +01:00
export const supabaseClient = createClient<Database>(
SUPABASE_URL,
SUPABASE_ANON_KEY,
{
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
)
/**
* 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()
}
})