'use client'; import { useCallback } from 'react'; import type { Provider } from '@supabase/supabase-js'; import { useSignInWithProvider } from '@kit/supabase/hooks/use-sign-in-with-provider'; import { If } from '@kit/ui/if'; import { LoadingOverlay } from '@kit/ui/loading-overlay'; import { Trans } from '@kit/ui/trans'; import { AuthErrorAlert } from './auth-error-alert'; import { AuthProviderButton } from './auth-provider-button'; export function OauthProviders(props: { inviteToken?: string; shouldCreateUser: boolean; enabledProviders: Provider[]; paths: { callback: string; returnPath: string; }; }) { const signInWithProviderMutation = useSignInWithProvider(); // we make the UI "busy" until the next page is fully loaded const loading = signInWithProviderMutation.isPending; const onSignInWithProvider = useCallback( async (signInRequest: () => Promise) => { const credential = await signInRequest(); if (!credential) { return Promise.reject(new Error('Failed to sign in with provider')); } }, [], ); const enabledProviders = props.enabledProviders; if (!enabledProviders?.length) { return null; } return ( <>
{enabledProviders.map((provider) => { return ( { const origin = window.location.origin; const queryParams = new URLSearchParams(); if (props.paths.returnPath) { queryParams.set('next', props.paths.returnPath); } if (props.inviteToken) { queryParams.set('invite_token', props.inviteToken); } const redirectPath = [ props.paths.callback, queryParams.toString(), ].join('?'); const redirectTo = [origin, redirectPath].join(''); const scopesOpts = provider === 'azure' ? { scopes: 'email', } : {}; const credentials = { provider, options: { shouldCreateUser: props.shouldCreateUser, redirectTo, ...scopesOpts, }, }; return onSignInWithProvider(() => signInWithProviderMutation.mutateAsync(credentials), ); }} > ); })}
); } function getProviderName(providerId: string) { const capitalize = (value: string) => value.slice(0, 1).toUpperCase() + value.slice(1); if (providerId.endsWith('.com')) { return capitalize(providerId.split('.com')[0]!); } return capitalize(providerId); }