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
import type { AuthProvider } from "@reactive-resume/auth/types";
import { i18n } from "@lingui/core";
import { t } from "@lingui/core/macro";
import { Trans } from "@lingui/react/macro";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useRouteContext } from "@tanstack/react-router";
import { Button } from "@reactive-resume/ui/components/button";
import { Switch } from "@reactive-resume/ui/components/switch";
import { toast } from "@reactive-resume/ui/components/toast";
import { SettingsRow, SettingsSection } from "../section";
import { getProviderName, useAuthAccounts, useAuthProviderActions, useEnabledProviders } from "./auth-hooks";
import { BackupCodesRow } from "./backup-codes";
import { SessionsRows } from "./sessions";
import { useDialogStore } from "@/dialogs/store";
import { useConfirm, usePrompt } from "@/hooks/use-confirm";
import { authClient } from "@/libs/auth/client";
import { getReadableErrorMessage } from "@/libs/error-message";
const SOCIAL: AuthProvider[] = ["apple", "google", "github", "linkedin", "custom"];
const monthYear = (date: Date) => i18n.date(date, { month: "long", year: "numeric" });
export function SecuritySection() {
const openDialog = useDialogStore((state) => state.openDialog);
const { hasAccount, getAccountByProviderId } = useAuthAccounts();
const { enabledProviders } = useEnabledProviders();
const { link, unlink } = useAuthProviderActions();
const { data: session } = authClient.useSession();
const password = getAccountByProviderId("credential");
const twoFactor = session?.user.twoFactorEnabled ?? false;
const social = SOCIAL.filter((provider) => provider in enabledProviders);
return (
<SettingsSection id="security" title={<Trans>Sign-in & security</Trans>}>
<SettingsRow
id="password"
title={<Trans>Password</Trans>}
description={
password ? (
<Trans>Last changed in {monthYear(new Date(password.updatedAt))}</Trans>
) : (
<Trans>You sign in without one</Trans>
)
}
>
{password ? (
<Button size="sm" variant="secondary" onClick={() => openDialog("auth.change-password", undefined)}>
<Trans>Change</Trans>
</Button>
) : "credential" in enabledProviders ? (
<Button size="sm" variant="secondary" onClick={() => openDialog("auth.set-password", undefined)}>
<Trans>Set a password</Trans>
</Button>
) : null}
</SettingsRow>
<div id="two-step" className="scroll-mt-6">
{!password && (
<SettingsRow
title={<Trans>Two-step verification</Trans>}
description={<Trans>Set a password to turn on two-step verification.</Trans>}
/>
)}
{password && (
<SettingsRow
title={<Trans>Two-step verification</Trans>}
description={<Trans>A code from an authenticator app at sign-in</Trans>}
>
<Switch
aria-label={t`Two-step verification`}
checked={twoFactor}
// Turning it on walks through the QR code and backup codes; turning it off asks for the password.
onCheckedChange={() =>
openDialog(twoFactor ? "auth.two-factor.disable" : "auth.two-factor.enable", undefined)
}
/>
</SettingsRow>
)}
{password && twoFactor && <BackupCodesRow />}
</div>
<Passkeys />
<SessionsRows />
<div id="linked-accounts" className="grid scroll-mt-6">
<h3 className="text-sm font-medium">
<Trans>Linked accounts</Trans>
</h3>
{social.length === 0 && (
<p className="text-[13px] text-ink-3">
<Trans>No social sign-in providers are available on this server.</Trans>
</p>
)}
{social.map((provider) => {
const account = getAccountByProviderId(provider);
const name =
provider === "custom" ? (enabledProviders.custom ?? getProviderName(provider)) : getProviderName(provider);
return (
<SettingsRow
key={provider}
title={name}
description={hasAccount(provider) ? <Trans>Connected</Trans> : <Trans>Not connected</Trans>}
>
{account ? (
<Button size="sm" variant="ghost" onClick={() => void unlink(provider, account.id)}>
<Trans>Disconnect</Trans>
</Button>
) : (
<Button size="sm" variant="secondary" onClick={() => void link(provider)}>
<Trans>Connect</Trans>
</Button>
)}
</SettingsRow>
);
})}
</div>
</SettingsSection>
);
}
const PASSKEYS_KEY = ["auth", "passkeys"];
function Passkeys() {
const queryClient = useQueryClient();
const { data: session } = authClient.useSession();
const smtpEnabled = useRouteContext({ strict: false }).flags?.smtpEnabled ?? false;
// The server adds passkeys only to verified addresses; say so instead of offering a button that always fails.
const verified = session?.user.emailVerified ?? true;
const prompt = usePrompt();
const confirm = useConfirm();
const { data: passkeys = [] } = useQuery({
queryKey: PASSKEYS_KEY,
queryFn: () => authClient.passkey.listUserPasskeys(),
select: ({ data }) => data ?? [],
});
const refresh = () => queryClient.invalidateQueries({ queryKey: PASSKEYS_KEY });
const failed = (error: unknown, fallback: string) =>
toast.add({ type: "error", description: getReadableErrorMessage(error, fallback) });
const add = useMutation({
mutationFn: async () => {
const { data, error } = await authClient.passkey.addPasskey();
if (error) return failed(error, t`Couldn't add the passkey. Try again.`);
await refresh();
const name = await prompt(t`Name this passkey`, {
description: t`A name helps you tell passkeys apart, like “Work laptop”.`,
defaultValue: "",
confirmText: t`Save name`,
});
const id = typeof data?.id === "string" ? data.id : null;
if (!id || !name?.trim()) return;
const renamed = await authClient.passkey.updatePasskey({ id, name: name.trim() });
if (renamed.error) return failed(renamed.error, t`Couldn't rename the passkey. Try again.`);
await refresh();
},
});
const remove = useMutation({
mutationFn: async (passkey: (typeof passkeys)[number]) => {
const name = passkey.name || t`Unnamed passkey`;
if (
!(await confirm(t`Remove “${name}”?`, {
description: t`You can no longer sign in with this passkey.`,
confirmText: t`Remove passkey`,
}))
)
return;
const { error } = await authClient.passkey.deletePasskey({ id: passkey.id });
if (error) return failed(error, t`Couldn't remove the passkey. Try again.`);
await refresh();
},
});
const rename = useMutation({
mutationFn: async (passkey: (typeof passkeys)[number]) => {
const name = await prompt(t`Name this passkey`, { defaultValue: passkey.name ?? "", confirmText: t`Save name` });
if (!name?.trim()) return;
const { error } = await authClient.passkey.updatePasskey({ id: passkey.id, name: name.trim() });
if (error) return failed(error, t`Couldn't rename the passkey. Try again.`);
await refresh();
},
});
return (
<div id="passkeys" className="grid scroll-mt-6">
<SettingsRow
title={<Trans>Passkeys</Trans>}
description={
verified ? (
<Trans>Sign in with your fingerprint, face or device PIN</Trans>
) : smtpEnabled ? (
<Trans>Verify your email to add a passkey. The link is in Account details above.</Trans>
) : (
<Trans>Passkeys need a verified email, which this server can't send.</Trans>
)
}
>
<Button size="sm" variant="secondary" loading={add.isPending} disabled={!verified} onClick={() => add.mutate()}>
<Trans>Add passkey</Trans>
</Button>
</SettingsRow>
{passkeys.length > 0 && (
<ul className="ms-4 grid border-s border-line ps-3">
{passkeys.map((passkey) => (
<li key={passkey.id} className="flex items-center gap-3 py-1.5 text-sm">
<span className="min-w-0 flex-1 truncate">{passkey.name || t`Unnamed passkey`}</span>
<Button size="sm" variant="ghost" disabled={rename.isPending} onClick={() => rename.mutate(passkey)}>
<Trans>Rename</Trans>
</Button>
<Button
size="sm"
variant="ghost"
disabled={remove.isPending}
aria-label={t`Remove ${passkey.name || t`Unnamed passkey`}`}
onClick={() => remove.mutate(passkey)}
>
<Trans>Remove</Trans>
</Button>
</li>
))}
</ul>
)}
</div>
);
}