prefer using providers conf from supabase instead of env

This commit is contained in:
2025-09-10 06:31:23 +03:00
parent 9f9508233d
commit 95452de88b
13 changed files with 382 additions and 33 deletions

View File

@@ -1,2 +1,3 @@
export * from './use-csrf-token';
export * from './use-current-locale-language-names';
export * from './use-auth-config';

View File

@@ -0,0 +1,76 @@
'use client';
import { useEffect, useState } from 'react';
import type { Provider } from '@supabase/supabase-js';
import { getCachedAuthConfig } from '../config/dynamic-auth.config';
import { authConfig } from '../config';
interface AuthConfig {
providers: {
password: boolean;
magicLink: boolean;
oAuth: Provider[];
};
}
interface UseAuthConfigResult {
config: AuthConfig | null;
loading: boolean;
error: Error | null;
refetch: () => Promise<void>;
}
export function useAuthConfig(): UseAuthConfigResult {
const [config, setConfig] = useState<AuthConfig | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const fetchConfig = async () => {
try {
setLoading(true);
setError(null);
const authConfig = await getCachedAuthConfig();
setConfig(authConfig);
} catch (err) {
console.error('Failed to fetch auth config', err);
setError(err instanceof Error ? err : new Error('Failed to fetch auth config'));
setConfig(authConfig);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchConfig();
}, []);
return {
config,
loading,
error,
refetch: fetchConfig,
};
}
export function useProviderEnabled(provider: 'password' | 'magicLink' | Provider) {
const { config, loading, error } = useAuthConfig();
const isEnabled = (() => {
if (!config) return false;
switch (provider) {
case 'password':
return config.providers.password;
case 'magicLink':
return config.providers.magicLink;
default:
return config.providers.oAuth.includes(provider);
}
})();
return {
enabled: isEnabled,
loading,
error,
};
}