From a5e4c0ef7503625ba130153ad8e51d706074c3e4 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Mon, 21 Mar 2022 00:46:15 -0700 Subject: [PATCH] client: use next middleware for redirects/rewrites based on auth; make preview 100% height always --- client/components/document/index.tsx | 3 ++- client/pages/_middleware.tsx | 31 ++++++++++++++++++++++++++++ client/pages/index.tsx | 1 - 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 client/pages/_middleware.tsx diff --git a/client/components/document/index.tsx b/client/components/document/index.tsx index a1040541..35a4b3ef 100644 --- a/client/components/document/index.tsx +++ b/client/components/document/index.tsx @@ -49,7 +49,8 @@ const DownloadButton = ({ rawLink }: { rawLink?: string }) => { const Document = ({ remove, editable, title, content, setTitle, setContent, initialTab = 'edit', skeleton, id }: Props) => { const codeEditorRef = useRef(null) const [tab, setTab] = useState(initialTab) - const height = editable ? "500px" : '100%' + // const height = editable ? "500px" : '100%' + const height = "100%"; const handleTabChange = (newTab: string) => { if (newTab === 'edit') { diff --git a/client/pages/_middleware.tsx b/client/pages/_middleware.tsx new file mode 100644 index 00000000..85b29366 --- /dev/null +++ b/client/pages/_middleware.tsx @@ -0,0 +1,31 @@ +import { NextFetchEvent, NextRequest, NextResponse } from 'next/server' + +const PUBLIC_FILE = /.(.*)$/ + +export function middleware(req: NextRequest, ev: NextFetchEvent) { + const pathname = req.nextUrl.pathname + // const isPageRequest = + // !PUBLIC_FILE.test(req.nextUrl.pathname) && + // !req.nextUrl.pathname.startsWith('/api') && + // // header added when next/link pre-fetches a route + // !req.headers.get('x-middleware-preflight') + + // If you're signed in we replace the home page with the new post page + if (pathname === '/') { + if (req.cookies['drift-token']) { + return NextResponse.rewrite(new URL(`/new`, req.url).href) + } + // If you're not signed in we redirect the new post page to the home page + } else if (pathname === '/new') { + if (!req.cookies['drift-token']) { + return NextResponse.redirect(new URL(`/`, req.url).href) + } + // If you're signed in we redirect the sign in page to the home page (which is the new page) + } else if (pathname === '/signin') { + if (req.cookies['drift-token']) { + return NextResponse.redirect(new URL(`/`, req.url).href) + } + } + + return NextResponse.next() +} diff --git a/client/pages/index.tsx b/client/pages/index.tsx index 9d59334f..771d175b 100644 --- a/client/pages/index.tsx +++ b/client/pages/index.tsx @@ -27,7 +27,6 @@ const Home = ({ theme, changeTheme, introContent }: Props) => { -