From 417789769173586568e532c17fa902775c2bc6c9 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Mon, 18 Apr 2022 14:48:08 -0700 Subject: [PATCH] client: redirect unauthed users from all /new paths --- client/components/post-page/index.tsx | 2 ++ client/lib/hooks/use-signed-in.ts | 2 -- client/pages/_middleware.tsx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/components/post-page/index.tsx b/client/components/post-page/index.tsx index 0d6ee01..b9599e9 100644 --- a/client/components/post-page/index.tsx +++ b/client/components/post-page/index.tsx @@ -18,6 +18,7 @@ import CreatedAgoBadge from "@components/badges/created-ago-badge" import Cookies from "js-cookie" import PasswordModalPage from "./password-modal-wrapper" import VisibilityControl from "@components/badges/visibility-control" +import useSharedState from "@lib/hooks/use-shared-state" type Props = { post: Post @@ -34,6 +35,7 @@ const PostPage = ({ post: initialPost, isProtected }: Props) => { const [isOwner] = useState( post.users ? post.users[0].id === Cookies.get("drift-userid") : false ) + const [signedIn] = useSharedState("signedIn") const router = useRouter() const isMobile = useMediaQuery("mobile") diff --git a/client/lib/hooks/use-signed-in.ts b/client/lib/hooks/use-signed-in.ts index f55ffab..60aacef 100644 --- a/client/lib/hooks/use-signed-in.ts +++ b/client/lib/hooks/use-signed-in.ts @@ -1,5 +1,4 @@ import Cookies from "js-cookie" -import { useRouter } from "next/router" import { useEffect } from "react" import useSharedState from "./use-shared-state" @@ -9,7 +8,6 @@ const useSignedIn = () => { typeof window === "undefined" ? false : !!Cookies.get("drift-token") ) const token = Cookies.get("drift-token") - const router = useRouter() const signin = (token: string) => { setSignedIn(true) // TODO: investigate SameSite / CORS cookie security diff --git a/client/pages/_middleware.tsx b/client/pages/_middleware.tsx index cba474a..0e8edac 100644 --- a/client/pages/_middleware.tsx +++ b/client/pages/_middleware.tsx @@ -45,7 +45,7 @@ export function middleware(req: NextRequest, event: NextFetchEvent) { return NextResponse.redirect(getURL("new")) } } else if (!signedIn) { - if (pathname === "/new") { + if (pathname.startsWith("/new")) { return NextResponse.redirect(getURL("signin")) } }