Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Setup: give the Resend call a deadline, use a known support domain

Claude9f01662701ac
diff --git a/src/app/api/setup/route.ts b/src/app/api/setup/route.ts
index 9c900ca..21b1e82 100644
--- a/src/app/api/setup/route.ts
+++ b/src/app/api/setup/route.ts
@@ -65,11 +65,30 @@ async function createAdminAccount(input: {
// goes wrong here (no Resend key yet, a key that may only send, the free
// plan's domain limit) is left for that page to explain, where the
// administrator can try again.
+// It is given a few seconds at most: the account already exists by now,
+// and a slow answer from Resend must not turn a finished setup into an
+// error the administrator cannot retry.
+const ADD_SENDER_DOMAIN_TIMEOUT_MS = 4_000;
+
async function addSenderDomain(): Promise<void> {
+ let timer: ReturnType<typeof setTimeout> | undefined;
+
try {
- await addEmailDomain();
+ await Promise.race([
+ addEmailDomain(),
+ new Promise<void>((resolve) => {
+ timer = setTimeout(() => {
+ console.error(
+ "Setup stopped waiting for Resend to add the sending domain.",
+ );
+ resolve();
+ }, ADD_SENDER_DOMAIN_TIMEOUT_MS);
+ }),
+ ]);
} catch (error) {
console.error("Setup could not add the sending domain to Resend:", error);
+ } finally {
+ clearTimeout(timer);
}
}
diff --git a/src/app/setup/page.tsx b/src/app/setup/page.tsx
index 1555a9f..9285671 100644
--- a/src/app/setup/page.tsx
+++ b/src/app/setup/page.tsx
@@ -33,7 +33,10 @@ export const metadata: Metadata = {
title: "Set up",
};
-async function loadView(): Promise<SetupShellView> {
+async function loadView(): Promise<{
+ storedSupportAddress: string | null;
+ view: SetupShellView;
+}> {
try {
const state = await getSetupState();
// Only an install that already has accounts cares who is signed in.
@@ -47,8 +50,9 @@ async function loadView(): Promise<SetupShellView> {
? await readStoredSettings()
: null;
- return resolveSetupView(state, {
- storedSupportAddress: stored?.supportAddress ?? null,
+ const storedSupportAddress = stored?.supportAddress ?? null;
+ const view = resolveSetupView(state, {
+ storedSupportAddress,
sessionUser: session
? {
email: session.user.email,
@@ -57,10 +61,12 @@ async function loadView(): Promise<SetupShellView> {
}
: null,
});
+
+ return { storedSupportAddress, view };
} catch (error) {
// No database connected at all. The page can still say so.
if (error instanceof ServiceConfigurationError) {
- return { view: "no_database" };
+ return { storedSupportAddress: null, view: { view: "no_database" } };
}
throw error;
@@ -72,7 +78,7 @@ async function loadView(): Promise<SetupShellView> {
export default async function SetupPage() {
await connection();
- const view = await loadView();
+ const { storedSupportAddress, view } = await loadView();
if (view.view === "complete") {
redirect("/login");
@@ -84,6 +90,9 @@ export default async function SetupPage() {
<SetupShell
hosted={isHostedMode()}
onVercel={process.env.VERCEL === "1"}
+ knownSupportAddress={
+ process.env.SUPPORT_EMAIL?.trim() || storedSupportAddress
+ }
resendDomain={process.env.RESEND_EMAIL_DOMAIN?.trim() || null}
siteHost={readSiteHost()}
view={view}
diff --git a/src/components/setup/setup-shell.tsx b/src/components/setup/setup-shell.tsx
index 2ddc58b..8012a9b 100644
--- a/src/components/setup/setup-shell.tsx
+++ b/src/components/setup/setup-shell.tsx
@@ -35,6 +35,11 @@ export type SetupShellView =
type SetupShellProps = {
hosted: boolean;
onVercel: boolean;
+ /**
+ * A support address setup does not ask for, because SUPPORT_EMAIL sets it
+ * or one is saved already. Its domain helps guess the sending domain.
+ */
+ knownSupportAddress: string | null;
/** RESEND_EMAIL_DOMAIN, set by the Resend integration on Vercel. */
resendDomain: string | null;
/** The host this site answers on, used to guess the sending domain. */
@@ -65,6 +70,7 @@ const emptyValues: SetupValues = {
export function SetupShell({
hosted,
+ knownSupportAddress,
onVercel,
resendDomain,
siteHost,
@@ -84,6 +90,7 @@ export function SetupShell({
<section className="panel w-full max-w-[520px] p-8 sm:p-10">
<SetupContent
hosted={hosted}
+ knownSupportAddress={knownSupportAddress}
onVercel={onVercel}
resendDomain={resendDomain}
siteHost={siteHost}
@@ -100,6 +107,7 @@ export function SetupShell({
function SetupContent({
hosted,
+ knownSupportAddress,
onVercel,
resendDomain,
siteHost,
@@ -191,6 +199,7 @@ function SetupContent({
return (
<SetupForm
hosted={hosted}
+ knownSupportAddress={knownSupportAddress}
resendDomain={resendDomain}
siteHost={siteHost}
view={view}
@@ -241,11 +250,13 @@ type SenderErrors = Partial<Record<keyof SenderAddressParts, string>>;
function SetupForm({
hosted,
+ knownSupportAddress,
resendDomain,
siteHost,
view,
}: {
hosted: boolean;
+ knownSupportAddress: string | null;
resendDomain: string | null;
siteHost: string | null;
view: Extract<SetupShellView, { view: "claim" | "create" }>;
@@ -280,7 +291,7 @@ function SetupForm({
const asksForSupport = view.asksForSupportAddress;
const suggestedRootDomain = asksForSender
? suggestSendingRootDomain({
- emails: [values.email, values.supportAddress],
+ emails: [values.email, values.supportAddress, knownSupportAddress],
siteHost,
})
: null;