From 856112e5e476ab92548ba78d7a1df3b748350c8d Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 25 Oct 2024 17:26:19 -0700 Subject: [PATCH] rm with auth SSR --- web/components/layout/auth/authLayout.tsx | 15 ++++++++++++-- .../templates/requestsV2/requestsPageV2.tsx | 9 ++++++++- web/pages/properties.tsx | 18 +---------------- web/pages/sessions.tsx | 13 +++++++----- web/pages/users/index.tsx | 20 +------------------ 5 files changed, 31 insertions(+), 44 deletions(-) diff --git a/web/components/layout/auth/authLayout.tsx b/web/components/layout/auth/authLayout.tsx index 314c84c8f3..ab6afe4445 100644 --- a/web/components/layout/auth/authLayout.tsx +++ b/web/components/layout/auth/authLayout.tsx @@ -1,7 +1,7 @@ /* eslint-disable @next/next/no-img-element */ import { useRouter } from "next/router"; -import { useMemo, useState } from "react"; +import { useEffect, useMemo, useState } from "react"; import { useAlertBanners, useChangelog } from "../../../services/hooks/admin"; import UpgradeProModal from "../../shared/upgradeProModal"; import { Row } from "../common"; @@ -12,6 +12,7 @@ import DemoModal from "./DemoModal"; import MainContent from "./MainContent"; import Sidebar from "./Sidebar"; import { ErrorBoundary } from "@/components/ui/error-boundary"; +import { useUser } from "@supabase/auth-helpers-react"; interface AuthLayoutProps { children: React.ReactNode; @@ -22,9 +23,9 @@ const AuthLayout = (props: AuthLayoutProps) => { const router = useRouter(); const { pathname } = router; const org = useOrg(); + const user = useUser(); const [open, setOpen] = useState(false); - const [showTermsModal, setShowTermsModal] = useState(false); const currentPage = useMemo(() => { const path = pathname.split("/")[1]; @@ -40,6 +41,16 @@ const AuthLayout = (props: AuthLayoutProps) => { const { changelog, isLoading: isChangelogLoading } = useChangelog(); + useEffect(() => { + if (!user) { + router.push("/signin?unauthorized=true"); + } + }, [router, user]); + + if (!user) { + return null; + } + return ( { isLive ? "text-green-500 animate-pulse" : "text-slate-500" )} size="sm_sleek" - onClick={() => setIsLive(!isLive)} + onClick={() => { + if (!isLive) { + searchParams.delete("t"); + setIsLive(true); + } else { + setIsLive(false); + } + }} >
{ - const { user } = props; - return ; }; @@ -19,15 +15,3 @@ Properties.getLayout = function getLayout(page: ReactElement) { }; export default Properties; - -export const getServerSideProps = withAuthSSR(async (options) => { - const { - userData: { user }, - } = options; - - return { - props: { - user, - }, - }; -}); diff --git a/web/pages/sessions.tsx b/web/pages/sessions.tsx index f5df7c70c7..f8a6dc168c 100644 --- a/web/pages/sessions.tsx +++ b/web/pages/sessions.tsx @@ -4,9 +4,10 @@ import { User } from "@supabase/auth-helpers-react"; import { SortDirection } from "../services/lib/sorts/requests/sorts"; import { ReactElement } from "react"; import SessionsPage from "../components/templates/sessions/sessionsPage"; +import { GetServerSideProps, GetServerSidePropsContext } from "next"; +import { ParsedUrlQuery } from "querystring"; interface SessionsProps { - user: User; currentPage: number; pageSize: number; sort: { @@ -37,16 +38,18 @@ Sessions.getLayout = function getLayout(page: ReactElement) { export default Sessions; -export const getServerSideProps = withAuthSSR(async (options) => { +export const getServerSideProps: GetServerSideProps< + SessionsProps, + ParsedUrlQuery +> = async (context: GetServerSidePropsContext) => { const { page, page_size, sortKey, sortDirection, isCustomProperty, tab } = - options.context.query; + context.query; const currentPage = parseInt(page as string, 10) || 1; const pageSize = parseInt(page_size as string, 10) || 10; return { props: { - user: options.userData.user, currentPage, pageSize, sort: { @@ -57,4 +60,4 @@ export const getServerSideProps = withAuthSSR(async (options) => { defaultIndex: tab ? parseInt(tab as string) : 0, }, }; -}); +}; diff --git a/web/pages/users/index.tsx b/web/pages/users/index.tsx index 61baa332d3..82f46bdec8 100644 --- a/web/pages/users/index.tsx +++ b/web/pages/users/index.tsx @@ -1,4 +1,3 @@ -import { User } from "@supabase/auth-helpers-nextjs"; import { GetServerSidePropsContext } from "next"; import { ReactElement } from "react"; @@ -8,7 +7,6 @@ import AuthLayout from "../../components/layout/auth/authLayout"; import { SupabaseServerWrapper } from "../../lib/wrappers/supabase"; interface UsersProps { - user: User; page: number; pageSize: number; sort: { @@ -19,7 +17,7 @@ interface UsersProps { } const Users = (props: UsersProps) => { - const { user, page, pageSize, sort } = props; + const { page, pageSize, sort } = props; return ; }; @@ -33,20 +31,6 @@ export default Users; export const getServerSideProps = async ( context: GetServerSidePropsContext ) => { - const supabase = new SupabaseServerWrapper(context).getClient(); - - const { - data: { session }, - } = await supabase.auth.getSession(); - - if (!session) - return { - redirect: { - destination: "/", - permanent: false, - }, - }; - const { page, page_size, sortKey, sortDirection, isCustomProperty } = context.query; @@ -55,8 +39,6 @@ export const getServerSideProps = async ( return { props: { - initialSession: session, - user: session.user, page: currentPage, pageSize: pageSize, sort: {