From 329d0640eb9bb433676cc3ca1ffe37d4e1efec15 Mon Sep 17 00:00:00 2001 From: Weves Date: Tue, 13 Jun 2023 22:43:55 -0700 Subject: [PATCH] Fix page crash when backend is down --- web/src/app/auth/login/page.tsx | 55 +++++++++++++++++++++++---------- web/src/app/page.tsx | 16 +++++++--- web/src/components/Button.tsx | 4 +-- web/src/lib/userSS.ts | 31 +++++++++++-------- 4 files changed, 71 insertions(+), 35 deletions(-) diff --git a/web/src/app/auth/login/page.tsx b/web/src/app/auth/login/page.tsx index 8fc573742..71e8c9780 100644 --- a/web/src/app/auth/login/page.tsx +++ b/web/src/app/auth/login/page.tsx @@ -1,17 +1,34 @@ +import { HealthCheckBanner } from "@/components/health/healthcheck"; import { DISABLE_AUTH } from "@/lib/constants"; +import { User } from "@/lib/types"; import { getGoogleOAuthUrlSS, getCurrentUserSS } from "@/lib/userSS"; import { redirect } from "next/navigation"; +const BUTTON_STYLE = + "group relative w-64 flex justify-center " + + "py-2 px-4 border border-transparent text-sm " + + "font-medium rounded-md text-white bg-red-600 " + + " mx-auto"; + const Page = async () => { // no need for any of the below if auth is disabled if (DISABLE_AUTH) { return redirect("/"); } - const [currentUser, authorizationUrl] = await Promise.all([ - getCurrentUserSS(), - getGoogleOAuthUrlSS(), - ]); + // catch cases where the backend is completely unreachable here + // without try / catch, will just raise an exception and the page + // will not render + let currentUser: User | null = null; + let authorizationUrl: string | null = null; + try { + [currentUser, authorizationUrl] = await Promise.all([ + getCurrentUserSS(), + getGoogleOAuthUrlSS(), + ]); + } catch (e) { + console.log(`Some fetch failed for the login page - ${e}`); + } // if user is already logged in, take them to the main app page if (currentUser && currentUser.is_active && currentUser.is_verified) { @@ -20,6 +37,9 @@ const Page = async () => { return (
+
+ +
@@ -28,18 +48,21 @@ const Page = async () => {
- - Sign in with Google - + {authorizationUrl ? ( + + Sign in with Google + + ) : ( + + )}
diff --git a/web/src/app/page.tsx b/web/src/app/page.tsx index 2974f01ae..1f5bd0ac6 100644 --- a/web/src/app/page.tsx +++ b/web/src/app/page.tsx @@ -18,19 +18,27 @@ export default async function Home() { }), ]; - const results = await Promise.all(tasks); + // catch cases where the backend is completely unreachable here + // without try / catch, will just raise an exception and the page + // will not render + let results: (User | Response | null)[] = [null, null]; + try { + results = await Promise.all(tasks); + } catch (e) { + console.log(`Some fetch failed for the main search page - ${e}`); + } const user = results[0] as User | null; - const connectorsResponse = results[1] as Response; + const connectorsResponse = results[1] as Response | null; if (!DISABLE_AUTH && !user) { return redirect("/auth/login"); } let connectors: Connector[] = []; - if (connectorsResponse.ok) { + if (connectorsResponse?.ok) { connectors = await connectorsResponse.json(); } else { - console.log(`Failed to fetch connectors - ${connectorsResponse.status}`); + console.log(`Failed to fetch connectors - ${connectorsResponse?.status}`); } // needs to be done in a non-client side component due to nextjs diff --git a/web/src/components/Button.tsx b/web/src/components/Button.tsx index 80d842b39..03ba92a1d 100644 --- a/web/src/components/Button.tsx +++ b/web/src/components/Button.tsx @@ -1,14 +1,14 @@ interface Props { - onClick: () => void; children: JSX.Element | string; + onClick?: () => void; disabled?: boolean; fullWidth?: boolean; className?: string; } export const Button = ({ - onClick, children, + onClick, disabled = false, fullWidth = false, className = "", diff --git a/web/src/lib/userSS.ts b/web/src/lib/userSS.ts index 602ffab22..df506ecbd 100644 --- a/web/src/lib/userSS.ts +++ b/web/src/lib/userSS.ts @@ -14,19 +14,24 @@ export const getGoogleOAuthUrlSS = async (): Promise => { // should be used server-side only export const getCurrentUserSS = async (): Promise => { - const response = await fetch(buildUrl("/users/me"), { - credentials: "include", - next: { revalidate: 0 }, - headers: { - cookie: cookies() - .getAll() - .map((cookie) => `${cookie.name}=${cookie.value}`) - .join("; "), - }, - }); - if (!response.ok) { + try { + const response = await fetch(buildUrl("/users/me"), { + credentials: "include", + next: { revalidate: 0 }, + headers: { + cookie: cookies() + .getAll() + .map((cookie) => `${cookie.name}=${cookie.value}`) + .join("; "), + }, + }); + if (!response.ok) { + return null; + } + const user = await response.json(); + return user; + } catch (e) { + console.log(`Error fetching user: ${e}`); return null; } - const user = await response.json(); - return user; };