feat: manually link social account

This commit is contained in:
Stephen Zhou
2024-12-31 19:55:06 +08:00
parent 7f080aa00b
commit fd373fb527
6 changed files with 146 additions and 9 deletions
@@ -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 (
<div className="space-y-2">
<p className="text-sm font-semibold">Auth Providers</p>
<div className="space-x-2">
{Object.keys(providers || {})
.filter((provider) => provider !== "credential")
.map((provider) => {
const account = accounts?.find((account) => account.provider === provider)
if (!account && IN_ELECTRON) return null
return (
<Button
key={provider}
variant={account ? "outline" : "primary"}
className="gap-2"
onClick={() => {
if (!account) {
linkSocial({
provider: provider as any,
})
return
}
// TODO: Implement delete social account after https://github.com/better-auth/better-auth/pull/1059
}}
>
<i className={cn("text-xl", authProvidersConfig[provider]?.iconClassName)} />
<span>{account ? account.profile?.email || account.profile?.name : "Link"}</span>
</Button>
)
})}
</div>
<p className="text-xs text-muted-foreground">{t("profile.link_social.description")}</p>
</div>
)
}
@@ -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() {
<>
<SettingsTitle />
<ProfileSettingForm />
<AccountManagement />
<UpdatePasswordForm />
</>
)
+11 -7
View File
@@ -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"),
}
}
+1
View File
@@ -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",
+8 -2
View File
@@ -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<AuthPlugin, { id: "getProviders" }>,
},
{
id: "createSession",
$InferServerPlugin: {} as (typeof authPlugins)[1],
$InferServerPlugin: {} as Extract<AuthPlugin, { id: "createSession" }>,
},
{
id: "getAccountInfo",
$InferServerPlugin: {} as Extract<AuthPlugin, { id: "getAccountInfo" }>,
},
inferAdditionalFields({
user: {
@@ -35,6 +40,7 @@ export const {
changePassword,
createSession,
forgetPassword,
getAccountInfo,
getProviders,
getSession,
linkSocial,
+75
View File
@@ -53,6 +53,32 @@ declare const authPlugins: ({
headers: Headers;
};
};
} | {
id: "getAccountInfo";
endpoints: {
getAccountInfo: {
<C extends [(better_call.Context<"/get-account-info", {
method: "GET";
}> | undefined)?]>(...ctx: C): Promise<C extends [{
asResponse: true;
}] ? Response : {
id: string;
provider: string;
profile: {
id?: string;
email?: string;
name?: string;
image?: string;
} | null;
}[] | null>;
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: {
<C extends [(better_call.Context<"/get-account-info", {
method: "GET";
}> | undefined)?]>(...ctx: C): Promise<C extends [{
asResponse: true;
}] ? Response : {
id: string;
provider: string;
profile: {
id?: string;
email?: string;
name?: string;
image?: string;
} | null;
}[] | null>;
path: "/get-account-info";
options: {
method: "GET";
};
method: better_call.Method | better_call.Method[];
headers: Headers;
};
} & {
updateUserccc: {
<C extends [(better_call.Context<"/update-user-ccc", {
@@ -9728,6 +9777,32 @@ declare const auth: {
headers: Headers;
};
};
} | {
id: "getAccountInfo";
endpoints: {
getAccountInfo: {
<C extends [(better_call.Context<"/get-account-info", {
method: "GET";
}> | undefined)?]>(...ctx: C): Promise<C extends [{
asResponse: true;
}] ? Response : {
id: string;
provider: string;
profile: {
id?: string;
email?: string;
name?: string;
image?: string;
} | null;
}[] | null>;
path: "/get-account-info";
options: {
method: "GET";
};
method: better_call.Method | better_call.Method[];
headers: Headers;
};
};
} | {
id: "updateUserccc";
endpoints: {