From fd373fb5271e4d9a31e37d49830d3fe3d4927922 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Tue, 31 Dec 2024 19:55:06 +0800 Subject: [PATCH] feat: manually link social account --- .../modules/profile/account-management.tsx | 49 ++++++++++++ .../src/pages/settings/(settings)/profile.tsx | 2 + apps/renderer/src/queries/auth.ts | 18 +++-- locales/settings/en.json | 1 + packages/shared/src/auth.ts | 10 ++- packages/shared/src/hono.ts | 75 +++++++++++++++++++ 6 files changed, 146 insertions(+), 9 deletions(-) create mode 100644 apps/renderer/src/modules/profile/account-management.tsx diff --git a/apps/renderer/src/modules/profile/account-management.tsx b/apps/renderer/src/modules/profile/account-management.tsx new file mode 100644 index 000000000..e1933e11e --- /dev/null +++ b/apps/renderer/src/modules/profile/account-management.tsx @@ -0,0 +1,49 @@ +import { Button } from "@follow/components/ui/button/index.js" +import { authProvidersConfig } from "@follow/constants" +import { linkSocial } from "@follow/shared/auth" +import { IN_ELECTRON } from "@follow/shared/constants" +import { cn } from "@follow/utils/utils" +import { useTranslation } from "react-i18next" + +import { useSocialAccounts } from "~/queries/auth" +import { useAuthProviders } from "~/queries/users" + +export function AccountManagement() { + const { t } = useTranslation("settings") + const { data: accounts } = useSocialAccounts() + const { data: providers } = useAuthProviders() + + return ( +
+

Auth Providers

+
+ {Object.keys(providers || {}) + .filter((provider) => provider !== "credential") + .map((provider) => { + const account = accounts?.find((account) => account.provider === provider) + if (!account && IN_ELECTRON) return null + return ( + + ) + })} +
+

{t("profile.link_social.description")}

+
+ ) +} diff --git a/apps/renderer/src/pages/settings/(settings)/profile.tsx b/apps/renderer/src/pages/settings/(settings)/profile.tsx index 0a3464f9d..5d2dc52c3 100644 --- a/apps/renderer/src/pages/settings/(settings)/profile.tsx +++ b/apps/renderer/src/pages/settings/(settings)/profile.tsx @@ -1,3 +1,4 @@ +import { AccountManagement } from "~/modules/profile/account-management" import { ProfileSettingForm } from "~/modules/profile/profile-setting-form" import { UpdatePasswordForm } from "~/modules/profile/update-password-form" import { SettingsTitle } from "~/modules/settings/title" @@ -16,6 +17,7 @@ export function Component() { <> + ) diff --git a/apps/renderer/src/queries/auth.ts b/apps/renderer/src/queries/auth.ts index 16751ddba..344a68e2c 100644 --- a/apps/renderer/src/queries/auth.ts +++ b/apps/renderer/src/queries/auth.ts @@ -1,4 +1,4 @@ -import { getSession, listAccounts } from "@follow/shared/auth" +import { getAccountInfo, getSession } from "@follow/shared/auth" import type { AuthSession } from "@follow/shared/hono" import type { FetchError } from "ofetch" @@ -7,22 +7,26 @@ import { defineQuery } from "~/lib/defineQuery" export const auth = { getSession: () => defineQuery(["auth", "session"], () => getSession()), - getAccounts: () => - defineQuery(["auth", "accounts"], async () => { - const accounts = await listAccounts() - return accounts.data as Array<{ id: string; provider: string }> - }), + getAccounts: () => defineQuery(["auth", "accounts"], () => getAccountInfo()), } export const useAccounts = () => { return useAuthQuery(auth.getAccounts()) } +export const useSocialAccounts = () => { + const accounts = useAccounts() + return { + ...accounts, + data: accounts.data?.data?.filter((account) => account.provider !== "credential"), + } +} + export const useHasPassword = () => { const accounts = useAccounts() return { ...accounts, - data: !!accounts.data?.find((account) => account.provider === "credential"), + data: !!accounts.data?.data?.find((account) => account.provider === "credential"), } } diff --git a/locales/settings/en.json b/locales/settings/en.json index e24602384..1b4acffd6 100644 --- a/locales/settings/en.json +++ b/locales/settings/en.json @@ -275,6 +275,7 @@ "profile.email.verified": "Verified", "profile.handle.description": "Your unique identifier.", "profile.handle.label": "Handle", + "profile.link_social.description": "You can currently only connect social accounts with the same email.", "profile.name.description": "Your public display name.", "profile.name.label": "Display Name", "profile.new_password.label": "New Password", diff --git a/packages/shared/src/auth.ts b/packages/shared/src/auth.ts index 5a37fb795..60e37b677 100644 --- a/packages/shared/src/auth.ts +++ b/packages/shared/src/auth.ts @@ -6,14 +6,19 @@ import { createAuthClient } from "better-auth/react" import { IN_ELECTRON, WEB_URL } from "./constants" +type AuthPlugin = (typeof authPlugins)[number] const serverPlugins = [ { id: "getProviders", - $InferServerPlugin: {} as (typeof authPlugins)[0], + $InferServerPlugin: {} as Extract, }, { id: "createSession", - $InferServerPlugin: {} as (typeof authPlugins)[1], + $InferServerPlugin: {} as Extract, + }, + { + id: "getAccountInfo", + $InferServerPlugin: {} as Extract, }, inferAdditionalFields({ user: { @@ -35,6 +40,7 @@ export const { changePassword, createSession, forgetPassword, + getAccountInfo, getProviders, getSession, linkSocial, diff --git a/packages/shared/src/hono.ts b/packages/shared/src/hono.ts index 98b5c9378..258abc711 100644 --- a/packages/shared/src/hono.ts +++ b/packages/shared/src/hono.ts @@ -53,6 +53,32 @@ declare const authPlugins: ({ headers: Headers; }; }; +} | { + id: "getAccountInfo"; + endpoints: { + getAccountInfo: { + | undefined)?]>(...ctx: C): Promise; + path: "/get-account-info"; + options: { + method: "GET"; + }; + method: better_call.Method | better_call.Method[]; + headers: Headers; + }; + }; } | { id: "updateUserccc"; endpoints: { @@ -9438,6 +9464,29 @@ declare const auth: { method: better_call.Method | better_call.Method[]; headers: Headers; }; + } & { + getAccountInfo: { + | undefined)?]>(...ctx: C): Promise; + path: "/get-account-info"; + options: { + method: "GET"; + }; + method: better_call.Method | better_call.Method[]; + headers: Headers; + }; } & { updateUserccc: { | undefined)?]>(...ctx: C): Promise; + path: "/get-account-info"; + options: { + method: "GET"; + }; + method: better_call.Method | better_call.Method[]; + headers: Headers; + }; + }; } | { id: "updateUserccc"; endpoints: {