"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Loader2, AlertTriangle } from "lucide-react"; import { authClient } from "@/lib/auth/auth-client"; import { useMutation } from "@tanstack/react-query"; import { toast } from "sonner"; import { useRouter } from "next/navigation"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { SetPasswordProfileProviderModal } from "./modal/set-password-modal"; import { Icon } from "@iconify/react"; import Image from "next/image"; import type { AuthProviderConfig } from "@/lib/auth/config"; import type { Account } from "@/db/schema/02_user"; interface ProfileProviderProps { accounts: Account[]; providers: AuthProviderConfig[]; } export function ProfileProviders({ accounts, providers, }: ProfileProviderProps) { const router = useRouter(); const totalConnected = accounts.length; const [loadingProvider, setLoadingProvider] = useState(null); const [isPasswordDialogOpen, setIsPasswordDialogOpen] = useState(false); const { mutate: unlinkAccount } = useMutation({ mutationFn: async (providerId: string) => { setLoadingProvider(providerId); const { error } = await authClient.unlinkAccount({ providerId }); if (error) throw error; }, onSuccess: () => { toast.success("Provider successfully unlinked!"); setLoadingProvider(null); router.refresh(); }, onError: () => { toast.error("An error occurred while unlinking provider."); setLoadingProvider(null); }, }); const { mutate: linkAccount } = useMutation({ mutationFn: async (provider: AuthProviderConfig) => { setLoadingProvider(provider.id); if (provider.type === "social") { await authClient.linkSocial({ provider: provider.id as string, callbackURL: "/dashboard", }); } }, onSuccess: () => { setLoadingProvider(null); router.refresh(); }, onError: () => { toast.error("An error occurred while linking provider."); setLoadingProvider(null); }, }); const enterpriseProviders = providers.filter( (p) => p.type === "sso" && p.id !== "passkey" && p.type !== "credential", ); const otherProviders = providers.filter( (p) => p.type !== "sso" && p.id !== "passkey" && p.type !== "credential", ); const renderProvider = (provider: AuthProviderConfig) => { const linkedAccount = accounts.find( (acc) => acc.providerId === provider.id, ); const isConnected = !!linkedAccount; const canUnlink = totalConnected > 1 || (totalConnected === 1 && !provider.isManual); const isLoading = loadingProvider === provider.id; const isUnlinkDisabled = !canUnlink || provider.allowUnlinking === false || provider.type === "sso"; const unlinkButton = ( ); let actionElement; if (provider.type === "sso") { actionElement = null; } else if (!isConnected) { actionElement = provider.id === "credential" ? ( ) : ( ); } else if (isUnlinkDisabled) { actionElement = (
{unlinkButton}

{provider.allowUnlinking === false ? "Unlinking is disabled for this provider." : "You cannot unlink your last authentication provider."}

); } else { actionElement = unlinkButton; } return (
{provider.icon.startsWith("/") || provider.icon.startsWith("http") ? ( {provider.id} ) : ( )}
{provider.title || provider.name} {isConnected && ( Active )}
{provider.description}
{actionElement}
); }; return (

Authentication

Manage how you access your account.

{enterpriseProviders.length > 0 && (

Enterprise Connection

{enterpriseProviders.map(renderProvider)}
)}

Standard Connections

{otherProviders.map(renderProvider)}
Linked providers allow you to log in to your account using any of these methods.
); }