prefer using providers conf from supabase instead of env
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export * from './use-csrf-token';
|
||||
export * from './use-current-locale-language-names';
|
||||
export * from './use-auth-config';
|
||||
|
||||
76
packages/shared/src/hooks/use-auth-config.ts
Normal file
76
packages/shared/src/hooks/use-auth-config.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user