From 598e1dd227e49510ae653a9a9a58c34610c46574 Mon Sep 17 00:00:00 2001 From: liqingwei Date: Mon, 15 Feb 2021 20:43:15 +0800 Subject: [PATCH] feat: check auth --- components/layout.tsx | 5 ++++- containers/page.ts | 14 ++++++++++---- pages/index.tsx | 19 +++++++++++++++++++ pages/page/[id].tsx | 8 +++----- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/components/layout.tsx b/components/layout.tsx index 7d1928a..cbf54c5 100644 --- a/components/layout.tsx +++ b/components/layout.tsx @@ -2,8 +2,11 @@ import { Editor } from './editor' import { List } from './list' import { PageListState } from '../containers/page-list' import { PageState } from '../containers/page' +import { useRouter } from 'next/router' export const Layout = () => { + const router = useRouter() + return (
@@ -15,7 +18,7 @@ export const Layout = () => {
- +
diff --git a/containers/page.ts b/containers/page.ts index 9a64858..01af32e 100644 --- a/containers/page.ts +++ b/containers/page.ts @@ -1,4 +1,4 @@ -import { useState } from 'react' +import { useEffect, useState } from 'react' import { createContainer } from 'unstated-next' import useFetch from 'use-http' @@ -10,13 +10,13 @@ export interface PageModel { content?: string } -const usePage = () => { +const usePage = (id?: string) => { const [page, setPage] = useState() - const { get, post } = useFetch(`/api/pages`) + const { get, post, response } = useFetch(`/api/pages`) const getById = async (id: string) => { - console.log(111, id) const data = await get(id) + console.log(111, response.headers) setPage({ id: '0', @@ -27,6 +27,12 @@ const usePage = () => { }) } + useEffect(() => { + if (id) { + getById(id) + } + }, [id]) + const savePage = async (data: PageModel) => { await post({ id: data.id, diff --git a/pages/index.tsx b/pages/index.tsx index 159507b..188e0f4 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,5 +1,24 @@ +import { GetServerSideProps } from 'next' +import { getSession } from 'next-auth/client' import { Layout } from '../components/layout' const IndexPage = () => +export const getServerSideProps: GetServerSideProps = async (ctx) => { + const session = await getSession(ctx) + + if (!session) { + return { + redirect: { + destination: '/api/auth/signin', + permanent: false, + }, + } + } + + return { + props: {}, + } +} + export default IndexPage diff --git a/pages/page/[id].tsx b/pages/page/[id].tsx index 8bbbef1..2663285 100644 --- a/pages/page/[id].tsx +++ b/pages/page/[id].tsx @@ -1,7 +1,5 @@ -import { Layout } from '../../components/layout' +import IndexPage, { getServerSideProps } from '..' -const PagePage = () => { - return -} +export { getServerSideProps } -export default PagePage +export default IndexPage