client: use next middleware for redirects/rewrites based on auth; make preview 100% height always

This commit is contained in:
Max Leiter 2022-03-21 00:46:15 -07:00
parent 594e903fe4
commit a5e4c0ef75
No known key found for this signature in database
GPG key ID: A3512F2F2F17EBDA
3 changed files with 33 additions and 2 deletions

View file

@ -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<HTMLTextAreaElement>(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') {

View file

@ -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()
}

View file

@ -27,7 +27,6 @@ const Home = ({ theme, changeTheme, introContent }: Props) => {
<Page className={styles.container} width="100%">
<PageSeo />
<Page.Header>
<Header theme={theme} changeTheme={changeTheme} />
</Page.Header>