44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { SUPABASE_ANON_KEY, SUPABASE_URL } from '@d3ro/core/supabase-config'
|
|
|
|
export interface AuthCapabilities {
|
|
signUpEnabled: boolean
|
|
emailEnabled: boolean
|
|
googleEnabled: boolean
|
|
githubEnabled: boolean
|
|
appleEnabled: boolean
|
|
}
|
|
|
|
interface AuthSettingsResponse {
|
|
disable_signup?: boolean
|
|
external?: {
|
|
email?: boolean
|
|
google?: boolean
|
|
github?: boolean
|
|
apple?: boolean
|
|
}
|
|
}
|
|
|
|
let cachedCapabilities: AuthCapabilities | null = null
|
|
|
|
export async function getAuthCapabilities(forceRefresh = false): Promise<AuthCapabilities> {
|
|
if (!forceRefresh && cachedCapabilities !== null) return cachedCapabilities
|
|
|
|
const response = await fetch(`${SUPABASE_URL}/auth/v1/settings`, {
|
|
headers: { apikey: SUPABASE_ANON_KEY },
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Auth settings request failed with status ${response.status}`)
|
|
}
|
|
|
|
const settings = await response.json() as AuthSettingsResponse
|
|
const external = settings.external ?? {}
|
|
cachedCapabilities = {
|
|
signUpEnabled: settings.disable_signup !== true,
|
|
emailEnabled: external.email === true,
|
|
googleEnabled: external.google === true,
|
|
githubEnabled: external.github === true,
|
|
appleEnabled: external.apple === true,
|
|
}
|
|
return cachedCapabilities
|
|
}
|