"use client"; import { useState, FormEvent } from "react"; import { useRouter } from "next/navigation"; import { Alert, Box, Button, Card, CardContent, Stack, TextField, Typography } from "@mui/material"; import { signIn } from "next-auth/react"; interface LinkAccountClientProps { provider: string; email: string; linkingId: string; } export default function LinkAccountClient({ provider, email, linkingId }: LinkAccountClientProps) { const router = useRouter(); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const handleLinkAccount = async (event: FormEvent) => { event.preventDefault(); setError(null); setLoading(true); try { // Call API to verify password and link account const response = await fetch("/api/auth/link-account", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ linkingId, password }) }); const data = await response.json(); if (!response.ok) { setError(data.error || "Failed to link account"); setLoading(false); return; } // Successfully linked - sign in with OAuth // The provider should now recognize the linked account await signIn(provider, { callbackUrl: "/" }); } catch (err) { setError("An error occurred while linking your account"); setLoading(false); } }; const handleUsePassword = () => { router.push("/login"); }; const providerName = provider.charAt(0).toUpperCase() + provider.slice(1); return ( Link Your Account An account with {email} already exists Would you like to link your {providerName} account to your existing account? Enter your password to confirm. {error && {error}} setPassword(e.target.value)} required fullWidth autoComplete="current-password" autoFocus disabled={loading} /> ); }