client: use next middleware for redirects/rewrites based on auth; make preview 100% height always
This commit is contained in:
parent
594e903fe4
commit
a5e4c0ef75
3 changed files with 33 additions and 2 deletions
|
@ -49,7 +49,8 @@ const DownloadButton = ({ rawLink }: { rawLink?: string }) => {
|
||||||
const Document = ({ remove, editable, title, content, setTitle, setContent, initialTab = 'edit', skeleton, id }: Props) => {
|
const Document = ({ remove, editable, title, content, setTitle, setContent, initialTab = 'edit', skeleton, id }: Props) => {
|
||||||
const codeEditorRef = useRef<HTMLTextAreaElement>(null)
|
const codeEditorRef = useRef<HTMLTextAreaElement>(null)
|
||||||
const [tab, setTab] = useState(initialTab)
|
const [tab, setTab] = useState(initialTab)
|
||||||
const height = editable ? "500px" : '100%'
|
// const height = editable ? "500px" : '100%'
|
||||||
|
const height = "100%";
|
||||||
|
|
||||||
const handleTabChange = (newTab: string) => {
|
const handleTabChange = (newTab: string) => {
|
||||||
if (newTab === 'edit') {
|
if (newTab === 'edit') {
|
||||||
|
|
31
client/pages/_middleware.tsx
Normal file
31
client/pages/_middleware.tsx
Normal 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()
|
||||||
|
}
|
|
@ -27,7 +27,6 @@ const Home = ({ theme, changeTheme, introContent }: Props) => {
|
||||||
<Page className={styles.container} width="100%">
|
<Page className={styles.container} width="100%">
|
||||||
<PageSeo />
|
<PageSeo />
|
||||||
|
|
||||||
|
|
||||||
<Page.Header>
|
<Page.Header>
|
||||||
<Header theme={theme} changeTheme={changeTheme} />
|
<Header theme={theme} changeTheme={changeTheme} />
|
||||||
</Page.Header>
|
</Page.Header>
|
||||||
|
|
Loading…
Reference in a new issue