A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!
TypeScript agent-skillsaidsh-pluginhacktoberfestjavascriptmcp-serverreactresume-builderself-hostedtypescript
fix(web): unlink social providers using internal account IDs (#3651)
Hoàng Anh Tuấnc3d67add443e
diff --git a/apps/web/src/features/settings/account/auth-hooks.ts b/apps/web/src/features/settings/account/auth-hooks.tsindex 6db97a001..296f09559 100644--- a/apps/web/src/features/settings/account/auth-hooks.ts+++ b/apps/web/src/features/settings/account/auth-hooks.ts@@ -69,12 +69,12 @@ export function useAuthAccounts() {});const getAccountByProviderId = useCallback(- (providerId: string) => accounts?.find((account) => account.providerId === providerId),+ (providerId: AuthProvider | (string & {})) => accounts?.find((account) => account.providerId === providerId),[accounts],);const hasAccount = useCallback(- (providerId: string) => !!getAccountByProviderId(providerId),+ (providerId: AuthProvider | (string & {})) => !!getAccountByProviderId(providerId),[getAccountByProviderId],);@@ -92,9 +92,15 @@ export function useAuthProviderActions() {const queryClient = useQueryClient();const link = useCallback(async (provider: AuthProvider) => {const providerName = getProviderName(provider);- const toastId = toast.add({ type: "loading", description: t`Linking your ${providerName} account...` });+ const toastId = toast.add({+ type: "loading",+ description: t`Linking your ${providerName} account...`,+ });- const { error } = await authClient.linkSocial({ provider, callbackURL: "/dashboard/settings/account" });+ const { error } = await authClient.linkSocial({+ provider,+ callbackURL: "/dashboard/settings/account",+ });if (error) {toast.add({@@ -114,6 +120,10 @@ export function useAuthProviderActions() {toast.close(toastId);}, []);+ /**+ * Unlinks a social account. Better Auth's /unlink-account endpoint expects+ * the internal database account ID (`account.id`), not `account.accountId`.+ */const unlink = useCallback(async (provider: AuthProvider, accountId: string) => {const providerName = getProviderName(provider);diff --git a/apps/web/src/features/settings/account/security.test.tsx b/apps/web/src/features/settings/account/security.test.tsxnew file mode 100644index 000000000..4f2d0a1ed--- /dev/null+++ b/apps/web/src/features/settings/account/security.test.tsx@@ -0,0 +1,101 @@+// @vitest-environment happy-dom++import type { ComponentProps } from "react";+import { fireEvent, render, screen, waitFor } from "@testing-library/react";+import { expect, it, vi } from "vite-plus/test";+import { i18n } from "@lingui/core";+import { I18nProvider } from "@lingui/react";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";++const unlinkMock = vi.hoisted(() => vi.fn().mockResolvedValue({ error: null }));+const listAccountsMock = vi.hoisted(() =>+ vi.fn().mockResolvedValue({+ data: [+ {+ id: "internal-account-uuid-123",+ providerId: "google",+ accountId: "google-external-sub-456",+ createdAt: new Date(),+ updatedAt: new Date(),+ },+ ],+ }),+);++vi.mock("@tanstack/react-router", () => ({+ useRouter: () => ({ invalidate: vi.fn() }),+ Link: ({ to, children, ...props }: ComponentProps<"a"> & { to: string }) => (+ <a href={to} {...props}>+ {children}+ </a>+ ),+}));++vi.mock("@/hooks/use-confirm", () => ({+ useConfirm: () => vi.fn(),+ usePrompt: () => vi.fn(),+}));++vi.mock("@/libs/auth/client", () => ({+ authClient: {+ useSession: () => ({ data: { user: { twoFactorEnabled: false } } }),+ listAccounts: listAccountsMock,+ unlinkAccount: unlinkMock,+ passkey: {+ listUserPasskeys: async () => ({ data: [] }),+ },+ },+}));++vi.mock("@/libs/orpc/client", () => ({+ orpc: {+ auth: {+ providers: {+ list: {+ queryOptions: () => ({+ queryKey: ["auth", "providers"],+ queryFn: async () => ({+ google: "Google",+ credential: "Password",+ }),+ }),+ },+ },+ },+ },+}));++vi.mock("@reactive-resume/ui/components/toast", () => ({+ toast: { add: vi.fn(), close: vi.fn() },+}));++i18n.loadAndActivate({ locale: "en", messages: {} });+const { SecuritySection } = await import("./security");++it("passes internal account ID (account.id) when unlinking a social provider", async () => {+ const queryClient = new QueryClient();++ render(+ <I18nProvider i18n={i18n}>+ <QueryClientProvider client={queryClient}>+ <SecuritySection />+ </QueryClientProvider>+ </I18nProvider>,+ );++ const disconnectButton = await screen.findByRole("button", { name: "Disconnect" });+ expect(disconnectButton).toBeInTheDocument();++ fireEvent.click(disconnectButton);++ await waitFor(() => {+ expect(unlinkMock).toHaveBeenCalledTimes(1);+ });++ expect(unlinkMock).toHaveBeenCalledWith({+ accountId: "internal-account-uuid-123",+ });+ expect(unlinkMock).not.toHaveBeenCalledWith({+ accountId: "google-external-sub-456",+ });+});diff --git a/apps/web/src/features/settings/account/security.tsx b/apps/web/src/features/settings/account/security.tsxindex 2b1e10a03..7d0a797e2 100644--- a/apps/web/src/features/settings/account/security.tsx+++ b/apps/web/src/features/settings/account/security.tsx@@ -80,7 +80,7 @@ export function SecuritySection() {description={hasAccount(provider) ? <Trans>Connected</Trans> : <Trans>Not connected</Trans>}>{account ? (- <Button size="sm" variant="ghost" onClick={() => void unlink(provider, account.accountId)}>+ <Button size="sm" variant="ghost" onClick={() => void unlink(provider, account.id)}><Trans>Disconnect</Trans></Button>) : (