Skip to content

Open-source status page, incident response and uptime monitoring

fix: server action login error message (#1693)

Maximilian Kaske8da3eba2e9ff
diff --git a/apps/status-page/src/app/(status-page)/[domain]/(auth)/login/_components/section-magic-link.tsx b/apps/status-page/src/app/(status-page)/[domain]/(auth)/login/_components/section-magic-link.tsx
index eb8fbaa9f..2d749af6b 100644
--- a/apps/status-page/src/app/(status-page)/[domain]/(auth)/login/_components/section-magic-link.tsx
+++ b/apps/status-page/src/app/(status-page)/[domain]/(auth)/login/_components/section-magic-link.tsx
@@ -13,6 +13,7 @@ import {
} from "@/components/content/section";
import { FormEmail, type FormValues } from "@/components/forms/form-email";
import { Button } from "@/components/ui/button";
+import { generateServerActionPromise } from "@/lib/server-actions";
import { Inbox } from "lucide-react";
import { useParams } from "next/navigation";
import { useState } from "react";
@@ -42,7 +43,7 @@ export function SectionMagicLink() {
try {
await new Promise((resolve) => setTimeout(resolve, 1000));
- await signInWithResendAction(formData);
+ await generateServerActionPromise(signInWithResendAction(formData));
setState("success");
} catch (error) {
setState("idle");
diff --git a/apps/status-page/src/app/(status-page)/[domain]/(auth)/login/actions.ts b/apps/status-page/src/app/(status-page)/[domain]/(auth)/login/actions.ts
index 1dd8ff455..57c1e32c2 100644
--- a/apps/status-page/src/app/(status-page)/[domain]/(auth)/login/actions.ts
+++ b/apps/status-page/src/app/(status-page)/[domain]/(auth)/login/actions.ts
@@ -2,6 +2,7 @@
import { signIn } from "@/lib/auth";
import { getQueryClient, trpc } from "@/lib/trpc/server";
+import { TRPCClientError } from "@trpc/client";
import { AuthError } from "next-auth";
import { isRedirectError } from "next/dist/client/components/redirect-error";
@@ -12,7 +13,10 @@ export async function signInWithResendAction(formData: FormData) {
const domain = formData.get("domain") as string;
if (!email || !redirectTo) {
- throw new Error("Email and redirectTo are required");
+ return {
+ success: false,
+ error: "Email and redirectTo are required",
+ };
}
const queryClient = getQueryClient();
@@ -26,26 +30,40 @@ export async function signInWithResendAction(formData: FormData) {
);
} catch (error) {
console.error("[SignIn] Email validation failed", error);
- throw new Error(
- "Your email domain is not authorized to access this status page",
- );
+ if (error instanceof TRPCClientError) {
+ return { success: false, error: error.message };
+ }
+ if (error instanceof Error) {
+ return { success: false, error: error.message };
+ }
+ return {
+ success: false,
+ error: "An unexpected error occurred during sign in",
+ };
}
await signIn("resend", {
email,
redirectTo,
});
+
+ return { success: true };
} catch (e) {
// NOTE: https://github.com/nextauthjs/next-auth/discussions/9389
- if (isRedirectError(e)) return;
+ if (isRedirectError(e)) {
+ return { success: true };
+ }
console.error("[SignIn] Error:", e);
if (e instanceof AuthError) {
- throw new Error(`Authentication error: ${e.type}`);
+ return { success: false, error: e.type };
}
if (e instanceof Error) {
- // Re-throw the error with the original message
- throw e;
+ return { success: false, error: e.message };
}
- throw new Error("An unexpected error occurred during sign in");
+
+ return {
+ success: false,
+ error: "An unexpected error occurred during sign in",
+ };
}
}
diff --git a/apps/status-page/src/lib/server-actions.ts b/apps/status-page/src/lib/server-actions.ts
new file mode 100644
index 000000000..d4f70dbc3
--- /dev/null
+++ b/apps/status-page/src/lib/server-actions.ts
@@ -0,0 +1,9 @@
+export async function generateServerActionPromise<T>(
+ promise: Promise<{ success: boolean; error?: string; data?: T }>,
+): Promise<T | undefined> {
+ const { success, data, error } = await promise;
+ if (!success) {
+ throw new Error(error);
+ }
+ return data;
+}