Option to regenerate Newt keys

This commit is contained in:
Pallavi Kumari
2025-10-26 21:00:46 +05:30
parent 42091e88cb
commit d32505a833
13 changed files with 491 additions and 41 deletions

View File

@@ -74,24 +74,24 @@ export default function CredentialsPage() {
setLoading(true);
try {
await api.post(`/client/${client?.clientId}`, {
await api.post(`/client/${client?.clientId}/regenerate-secret`, {
olmId: clientDefaults?.olmId,
secret: clientDefaults?.olmSecret,
});
toast({
title: t("clientUpdated"),
description: t("clientUpdatedDescription")
title: t("credentialsSaved"),
description: t("credentialsSavedDescription")
});
router.refresh();
} catch (e) {
toast({
variant: "destructive",
title: t("clientUpdateFailed"),
title: t("credentialsSaveError"),
description: formatAxiosError(
e,
t("clientUpdateError")
t("credentialsSaveErrorDescription")
)
});
} finally {
@@ -107,7 +107,7 @@ export default function CredentialsPage() {
{t("generatedcredentials")}
</SettingsSectionTitle>
<SettingsSectionDescription>
{t("regenerateClientCredentials")}
{t("regenerateCredentials")}
</SettingsSectionDescription>
</SettingsSectionHeader>

View File

@@ -7,6 +7,7 @@ import ClientInfoCard from "../../../../../components/ClientInfoCard";
import ClientProvider from "@app/providers/ClientProvider";
import { redirect } from "next/navigation";
import { HorizontalTabs } from "@app/components/HorizontalTabs";
import { getTranslations } from "next-intl/server";
type SettingsLayoutProps = {
children: React.ReactNode;
@@ -30,13 +31,15 @@ export default async function SettingsLayout(props: SettingsLayoutProps) {
redirect(`/${params.orgId}/settings/clients`);
}
const t = await getTranslations();
const navItems = [
{
title: "General",
title: t('general'),
href: `/{orgId}/settings/clients/{clientId}/general`
},
{
title: "Credentials",
title: t('credentials'),
href: `/{orgId}/settings/clients/{clientId}/credentials`
}
];

View File

@@ -0,0 +1,212 @@
"use client";
import { useEffect, useState } from "react";
import {
SettingsContainer,
SettingsSection,
SettingsSectionBody,
SettingsSectionDescription,
SettingsSectionHeader,
SettingsSectionTitle
} from "@app/components/Settings";
import { Button } from "@app/components/ui/button";
import { Alert, AlertDescription, AlertTitle } from "@app/components/ui/alert";
import { InfoIcon } from "lucide-react";
import { createApiClient, formatAxiosError } from "@app/lib/api";
import { useEnvContext } from "@app/hooks/useEnvContext";
import { toast } from "@app/hooks/useToast";
import { useParams, useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { InfoSection, InfoSectionContent, InfoSections, InfoSectionTitle } from "@app/components/InfoSection";
import CopyToClipboard from "@app/components/CopyToClipboard";
import { PickSiteDefaultsResponse } from "@server/routers/site";
import { useSiteContext } from "@app/hooks/useSiteContext";
export default function CredentialsPage() {
const { env } = useEnvContext();
const api = createApiClient({ env });
const { orgId } = useParams();
const router = useRouter();
const t = useTranslations();
const [newtId, setNewtId] = useState("");
const [newtSecret, setNewtSecret] = useState("");
const { site, updateSite } = useSiteContext();
const [siteDefaults, setSiteDefaults] =
useState<PickSiteDefaultsResponse | null>(null);
const [loading, setLoading] = useState(false);
const [saving, setSaving] = useState(false);
// Clear credentials when user leaves/reloads
useEffect(() => {
const clearCreds = () => {
setNewtId("");
setNewtSecret("");
};
window.addEventListener("beforeunload", clearCreds);
return () => window.removeEventListener("beforeunload", clearCreds);
}, []);
const handleRegenerate = async () => {
try {
setLoading(true);
await api
.get(`/org/${orgId}/pick-site-defaults`)
.then((res) => {
if (res && res.status === 200) {
const data = res.data.data;
setSiteDefaults(data);
const newtId = data.newtId;
const newtSecret = data.newtSecret;
setNewtId(newtId);
setNewtSecret(newtSecret);
}
});
} finally {
setLoading(false);
}
};
const handleSave = async () => {
setLoading(true);
try {
await api.post(`/site/${site?.siteId}/regenerate-secret`, {
newtId: siteDefaults?.newtId,
newtSecret: siteDefaults?.newtSecret,
});
toast({
title: t("credentialsSaved"),
description: t("credentialsSavedDescription")
});
router.refresh();
} catch (e) {
toast({
variant: "destructive",
title: t("credentialsSaveError"),
description: formatAxiosError(
e,
t("credentialsSaveErrorDescription")
)
});
} finally {
setLoading(false);
}
};
return (
<SettingsContainer>
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
{t("generatedcredentials")}
</SettingsSectionTitle>
<SettingsSectionDescription>
{t("regenerateCredentials")}
</SettingsSectionDescription>
</SettingsSectionHeader>
<SettingsSectionBody>
{!siteDefaults ? (
<Button
onClick={handleRegenerate}
loading={loading}
disabled={loading}
>
{t("regeneratecredentials")}
</Button>
) : (
<>
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
{t("siteNewtCredentials")}
</SettingsSectionTitle>
<SettingsSectionDescription>
{t(
"siteNewtCredentialsDescription"
)}
</SettingsSectionDescription>
</SettingsSectionHeader>
<SettingsSectionBody>
<InfoSections cols={3}>
<InfoSection>
<InfoSectionTitle>
{t("newtEndpoint")}
</InfoSectionTitle>
<InfoSectionContent>
<CopyToClipboard
text={
env.app.dashboardUrl
}
/>
</InfoSectionContent>
</InfoSection>
<InfoSection>
<InfoSectionTitle>
{t("newtId")}
</InfoSectionTitle>
<InfoSectionContent>
<CopyToClipboard
text={newtId}
/>
</InfoSectionContent>
</InfoSection>
<InfoSection>
<InfoSectionTitle>
{t("newtSecretKey")}
</InfoSectionTitle>
<InfoSectionContent>
<CopyToClipboard
text={newtSecret}
/>
</InfoSectionContent>
</InfoSection>
</InfoSections>
<Alert variant="neutral" className="mt-4">
<InfoIcon className="h-4 w-4" />
<AlertTitle className="font-semibold">
{t("copyandsavethesecredentials")}
</AlertTitle>
<AlertDescription>
{t(
"copyandsavethesecredentialsdescription"
)}
</AlertDescription>
</Alert>
</SettingsSectionBody>
</SettingsSection>
<div className="flex justify-end mt-6 space-x-2">
<Button
variant="outline"
onClick={() => {
setNewtId("");
setNewtSecret("");
}}
>
{t("cancel")}
</Button>
<Button
onClick={handleSave}
loading={saving}
disabled={saving}
>
{t("savecredentials")}
</Button>
</div>
</>
)}
</SettingsSectionBody>
</SettingsSection>
</SettingsContainer>
);
}

View File

@@ -36,6 +36,10 @@ export default async function SettingsLayout(props: SettingsLayoutProps) {
{
title: t('general'),
href: "/{orgId}/settings/sites/{niceId}/general"
},
{
title: t('credentials'),
href: "/{orgId}/settings/sites/{niceId}/credentials"
}
];

View File

@@ -1,7 +1,6 @@
"use client";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { InfoIcon } from "lucide-react";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { useClientContext } from "@app/hooks/useClientContext";
import {
InfoSection,
@@ -19,9 +18,7 @@ export default function SiteInfoCard({}: ClientInfoCardProps) {
return (
<Alert>
<InfoIcon className="h-4 w-4" />
<AlertTitle className="font-semibold">{t("clientInformation")}</AlertTitle>
<AlertDescription className="mt-4">
<AlertDescription>
<InfoSections cols={2}>
<>
<InfoSection>