Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

feat(2FA): add user security settings page

Rudy B.1b781922537a
diff --git a/src/app/(main)/settings/SettingsNav.tsx b/src/app/(main)/settings/SettingsNav.tsx
index 8c97b9d6c..8102ad7a6 100644
--- a/src/app/(main)/settings/SettingsNav.tsx
+++ b/src/app/(main)/settings/SettingsNav.tsx
@@ -2,7 +2,7 @@ import { Column, Focusable, Row, Text, Tooltip, TooltipTrigger } from '@umami/re
import Link from '@/components/common/Link';
import { IconLabel } from '@/components/common/IconLabel';
import { useMessages, useNavigation } from '@/components/hooks';
-import { ArrowLeft, Settings2, UserCircle, Users } from '@/components/icons';
+import { ArrowLeft, Settings2, ShieldCheck, UserCircle, Users } from '@/components/icons';
export function SettingsNav({
isCollapsed,
@@ -41,6 +41,12 @@ export function SettingsNav({
path: renderUrl('/settings/teams'),
icon: <Users />,
},
+ {
+ id: 'security',
+ label: t(labels.security),
+ path: renderUrl('/settings/security'),
+ icon: <ShieldCheck />,
+ },
],
},
];
diff --git a/src/app/(main)/settings/security/UserSecurityPage.tsx b/src/app/(main)/settings/security/UserSecurityPage.tsx
new file mode 100644
index 000000000..2e23786e5
--- /dev/null
+++ b/src/app/(main)/settings/security/UserSecurityPage.tsx
@@ -0,0 +1,94 @@
+'use client';
+import { Column, Row, Switch, Text, Tooltip, TooltipTrigger } from '@umami/react-zen';
+import { useState } from 'react';
+import { Badge } from '@/components/common/Badge';
+import { PageBody } from '@/components/common/PageBody';
+import { PageHeader } from '@/components/common/PageHeader';
+import { Panel } from '@/components/common/Panel';
+import { useMessages, useTwoFactorStatusQuery } from '@/components/hooks';
+import { TwoFactorDisableModal } from '@/components/modals/TwoFactorDisableModal';
+import { TwoFactorSetupModal } from '@/components/modals/TwoFactorSetupModal';
+import { useQueryClient } from '@tanstack/react-query';
+
+export function UserSecurityPage() {
+ const { t, labels, messages } = useMessages();
+ const { data: status, isLoading } = useTwoFactorStatusQuery(true);
+ const queryClient = useQueryClient();
+ const [showSetup, setShowSetup] = useState(false);
+ const [showDisable, setShowDisable] = useState(false);
+
+ const isEnabled = status?.isEnabled ?? false;
+ const isRequired = status?.isRequired ?? false;
+
+ const handleToggle = (value: boolean) => {
+ if (value) {
+ setShowSetup(true);
+ } else {
+ setShowDisable(true);
+ }
+ };
+
+ const handleSetupClose = () => {
+ setShowSetup(false);
+ queryClient.invalidateQueries({ queryKey: ['2fa-status'] });
+ };
+
+ const handleDisableSuccess = () => {
+ setShowDisable(false);
+ queryClient.invalidateQueries({ queryKey: ['2fa-status'] });
+ };
+
+ if (isLoading) return null;
+
+ return (
+ <PageBody>
+ <Column gap="6">
+ <PageHeader title={t(labels.security)} />
+ <Panel>
+ <Column gap="4">
+ <Row alignItems="center" gap="3">
+ <Text weight="bold">{t(labels.twoFactorAuth)}</Text>
+ {isEnabled ? (
+ <Badge variant="good">{t(labels.twoFactorActive)}</Badge>
+ ) : (
+ <Badge variant="gray">{t(labels.twoFactorStatusNotConfigured)}</Badge>
+ )}
+ </Row>
+
+ {isEnabled ? (
+ <Text>{t(messages.twoFactorActiveDescription)}</Text>
+ ) : (
+ <Text>{t(messages.twoFactorUserDescription)}</Text>
+ )}
+
+ <TooltipTrigger isDisabled={!(isEnabled && isRequired)}>
+ <Row alignItems="center" gap="3">
+ <Switch
+ isSelected={isEnabled}
+ isDisabled={isEnabled && isRequired}
+ onChange={handleToggle}
+ />
+ <Text>{t(labels.twoFactorEnable)}</Text>
+ </Row>
+ <Tooltip>{t(messages.twoFactorRequiredMessage)}</Tooltip>
+ </TooltipTrigger>
+
+ {isEnabled && isRequired && (
+ <Text size="sm" color="muted">
+ {t(messages.twoFactorRequiredMessage)}
+ </Text>
+ )}
+ </Column>
+ </Panel>
+ </Column>
+
+ {showSetup && <TwoFactorSetupModal required={false} onClose={handleSetupClose} />}
+ {showDisable && (
+ <TwoFactorDisableModal
+ onClose={() => setShowDisable(false)}
+ onSuccess={handleDisableSuccess}
+ />
+ )}
+ </PageBody>
+ );
+}
diff --git a/src/app/(main)/settings/security/page.tsx b/src/app/(main)/settings/security/page.tsx
new file mode 100644
index 000000000..e955d0eb5
--- /dev/null
+++ b/src/app/(main)/settings/security/page.tsx
@@ -0,0 +1,10 @@
+import type { Metadata } from 'next';
+import { UserSecurityPage } from './UserSecurityPage';
+
+export default function () {
+ return <UserSecurityPage />;
+}
+
+export const metadata: Metadata = {
+ title: 'Security',
+};