prettier fix
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Provider } from '@supabase/supabase-js';
|
||||
|
||||
import authConfig from './auth.config';
|
||||
|
||||
type SupabaseExternalProvider = Provider | 'email';
|
||||
@@ -10,7 +11,10 @@ interface SupabaseAuthSettings {
|
||||
|
||||
export class AuthProvidersService {
|
||||
private supabaseUrl: string;
|
||||
private cache: Map<string, { data: SupabaseAuthSettings; timestamp: number }> = new Map();
|
||||
private cache: Map<
|
||||
string,
|
||||
{ data: SupabaseAuthSettings; timestamp: number }
|
||||
> = new Map();
|
||||
private readonly CACHE_TTL = 5 * 60 * 1000; // 5 minutes
|
||||
|
||||
constructor(supabaseUrl: string) {
|
||||
@@ -21,7 +25,7 @@ export class AuthProvidersService {
|
||||
try {
|
||||
const cacheKey = 'auth-settings';
|
||||
const cached = this.cache.get(cacheKey);
|
||||
|
||||
|
||||
if (cached && Date.now() - cached.timestamp < this.CACHE_TTL) {
|
||||
return cached.data;
|
||||
}
|
||||
@@ -31,22 +35,28 @@ export class AuthProvidersService {
|
||||
throw new Error('NEXT_PUBLIC_SUPABASE_ANON_KEY is required');
|
||||
}
|
||||
|
||||
const response = await fetch(`${this.supabaseUrl}/auth/v1/settings?apikey=${anonKey}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
const response = await fetch(
|
||||
`${this.supabaseUrl}/auth/v1/settings?apikey=${anonKey}`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
});
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
console.warn('Failed to fetch auth settings from Supabase:', response.status);
|
||||
console.warn(
|
||||
'Failed to fetch auth settings from Supabase:',
|
||||
response.status,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
const settings: SupabaseAuthSettings = await response.json();
|
||||
|
||||
|
||||
this.cache.set(cacheKey, { data: settings, timestamp: Date.now() });
|
||||
|
||||
|
||||
return settings;
|
||||
} catch (error) {
|
||||
console.warn('Error fetching auth settings from Supabase:', error);
|
||||
@@ -54,7 +64,11 @@ export class AuthProvidersService {
|
||||
}
|
||||
}
|
||||
|
||||
isPasswordEnabled({ settings }: { settings: SupabaseAuthSettings | null }): boolean {
|
||||
isPasswordEnabled({
|
||||
settings,
|
||||
}: {
|
||||
settings: SupabaseAuthSettings | null;
|
||||
}): boolean {
|
||||
if (settings) {
|
||||
return settings.external.email === true && !settings.disable_signup;
|
||||
}
|
||||
@@ -62,7 +76,11 @@ export class AuthProvidersService {
|
||||
return process.env.NEXT_PUBLIC_AUTH_PASSWORD === 'true';
|
||||
}
|
||||
|
||||
isMailerAutoconfirmEnabled({ settings }: { settings: SupabaseAuthSettings | null }): boolean {
|
||||
isMailerAutoconfirmEnabled({
|
||||
settings,
|
||||
}: {
|
||||
settings: SupabaseAuthSettings | null;
|
||||
}): boolean {
|
||||
return settings?.mailer_autoconfirm === true;
|
||||
}
|
||||
|
||||
@@ -84,11 +102,17 @@ export class AuthProvidersService {
|
||||
return false;
|
||||
}
|
||||
|
||||
getEnabledOAuthProviders({ settings }: { settings: SupabaseAuthSettings | null }): SupabaseExternalProvider[] {
|
||||
getEnabledOAuthProviders({
|
||||
settings,
|
||||
}: {
|
||||
settings: SupabaseAuthSettings | null;
|
||||
}): SupabaseExternalProvider[] {
|
||||
const enabledProviders: SupabaseExternalProvider[] = [];
|
||||
|
||||
|
||||
if (settings && settings.external) {
|
||||
for (const [providerName, isEnabled] of Object.entries(settings.external)) {
|
||||
for (const [providerName, isEnabled] of Object.entries(
|
||||
settings.external,
|
||||
)) {
|
||||
if (isEnabled && providerName !== 'email') {
|
||||
enabledProviders.push(providerName as SupabaseExternalProvider);
|
||||
}
|
||||
@@ -98,19 +122,27 @@ export class AuthProvidersService {
|
||||
|
||||
const potentialProviders: SupabaseExternalProvider[] = ['keycloak'];
|
||||
const enabledFallback: SupabaseExternalProvider[] = [];
|
||||
|
||||
|
||||
for (const provider of potentialProviders) {
|
||||
if (provider !== 'email' && this.isOAuthProviderEnabled({ provider, settings })) {
|
||||
if (
|
||||
provider !== 'email' &&
|
||||
this.isOAuthProviderEnabled({ provider, settings })
|
||||
) {
|
||||
enabledFallback.push(provider);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return enabledFallback;
|
||||
}
|
||||
|
||||
async getAuthConfig() {
|
||||
const settings = await this.fetchAuthSettings();
|
||||
const [passwordEnabled, magicLinkEnabled, oAuthProviders, isMailerAutoconfirmEnabled] = await Promise.all([
|
||||
const [
|
||||
passwordEnabled,
|
||||
magicLinkEnabled,
|
||||
oAuthProviders,
|
||||
isMailerAutoconfirmEnabled,
|
||||
] = await Promise.all([
|
||||
this.isPasswordEnabled({ settings }),
|
||||
this.isMagicLinkEnabled(),
|
||||
this.getEnabledOAuthProviders({ settings }),
|
||||
@@ -135,10 +167,10 @@ export class AuthProvidersService {
|
||||
|
||||
export function createAuthProvidersService(): AuthProvidersService {
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
||||
|
||||
|
||||
if (!supabaseUrl) {
|
||||
throw new Error('NEXT_PUBLIC_SUPABASE_URL is required');
|
||||
}
|
||||
|
||||
|
||||
return new AuthProvidersService(supabaseUrl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user