Openstatus www.openstatus.dev

fix: server action login error message (#1693)

authored by

Maximilian Kaske and committed by
GitHub
8da3eba2 fa3744a0

+38 -10
+2 -1
apps/status-page/src/app/(status-page)/[domain]/(auth)/login/_components/section-magic-link.tsx
··· 13 13 } from "@/components/content/section"; 14 14 import { FormEmail, type FormValues } from "@/components/forms/form-email"; 15 15 import { Button } from "@/components/ui/button"; 16 + import { generateServerActionPromise } from "@/lib/server-actions"; 16 17 import { Inbox } from "lucide-react"; 17 18 import { useParams } from "next/navigation"; 18 19 import { useState } from "react"; ··· 42 43 43 44 try { 44 45 await new Promise((resolve) => setTimeout(resolve, 1000)); 45 - await signInWithResendAction(formData); 46 + await generateServerActionPromise(signInWithResendAction(formData)); 46 47 setState("success"); 47 48 } catch (error) { 48 49 setState("idle");
+27 -9
apps/status-page/src/app/(status-page)/[domain]/(auth)/login/actions.ts
··· 2 2 3 3 import { signIn } from "@/lib/auth"; 4 4 import { getQueryClient, trpc } from "@/lib/trpc/server"; 5 + import { TRPCClientError } from "@trpc/client"; 5 6 import { AuthError } from "next-auth"; 6 7 import { isRedirectError } from "next/dist/client/components/redirect-error"; 7 8 ··· 12 13 const domain = formData.get("domain") as string; 13 14 14 15 if (!email || !redirectTo) { 15 - throw new Error("Email and redirectTo are required"); 16 + return { 17 + success: false, 18 + error: "Email and redirectTo are required", 19 + }; 16 20 } 17 21 18 22 const queryClient = getQueryClient(); ··· 26 30 ); 27 31 } catch (error) { 28 32 console.error("[SignIn] Email validation failed", error); 29 - throw new Error( 30 - "Your email domain is not authorized to access this status page", 31 - ); 33 + if (error instanceof TRPCClientError) { 34 + return { success: false, error: error.message }; 35 + } 36 + if (error instanceof Error) { 37 + return { success: false, error: error.message }; 38 + } 39 + return { 40 + success: false, 41 + error: "An unexpected error occurred during sign in", 42 + }; 32 43 } 33 44 34 45 await signIn("resend", { 35 46 email, 36 47 redirectTo, 37 48 }); 49 + 50 + return { success: true }; 38 51 } catch (e) { 39 52 // NOTE: https://github.com/nextauthjs/next-auth/discussions/9389 40 - if (isRedirectError(e)) return; 53 + if (isRedirectError(e)) { 54 + return { success: true }; 55 + } 41 56 console.error("[SignIn] Error:", e); 42 57 if (e instanceof AuthError) { 43 - throw new Error(`Authentication error: ${e.type}`); 58 + return { success: false, error: e.type }; 44 59 } 45 60 if (e instanceof Error) { 46 - // Re-throw the error with the original message 47 - throw e; 61 + return { success: false, error: e.message }; 48 62 } 49 - throw new Error("An unexpected error occurred during sign in"); 63 + 64 + return { 65 + success: false, 66 + error: "An unexpected error occurred during sign in", 67 + }; 50 68 } 51 69 }
+9
apps/status-page/src/lib/server-actions.ts
··· 1 + export async function generateServerActionPromise<T>( 2 + promise: Promise<{ success: boolean; error?: string; data?: T }>, 3 + ): Promise<T | undefined> { 4 + const { success, data, error } = await promise; 5 + if (!success) { 6 + throw new Error(error); 7 + } 8 + return data; 9 + }