feat: check auth

This commit is contained in:
liqingwei 2021-02-15 20:43:15 +08:00
parent 834546c787
commit 598e1dd227
4 changed files with 36 additions and 10 deletions

View File

@ -2,8 +2,11 @@ import { Editor } from './editor'
import { List } from './list' import { List } from './list'
import { PageListState } from '../containers/page-list' import { PageListState } from '../containers/page-list'
import { PageState } from '../containers/page' import { PageState } from '../containers/page'
import { useRouter } from 'next/router'
export const Layout = () => { export const Layout = () => {
const router = useRouter()
return ( return (
<section className="flex h-screen"> <section className="flex h-screen">
<aside className="w-10 bg-gray-200">toolbar</aside> <aside className="w-10 bg-gray-200">toolbar</aside>
@ -15,7 +18,7 @@ export const Layout = () => {
<main className="flex-auto overflow-y-auto"> <main className="flex-auto overflow-y-auto">
<nav className="fixed bg-white w-full z-10 p-2 text-sm"></nav> <nav className="fixed bg-white w-full z-10 p-2 text-sm"></nav>
<article className="m-auto pt-40 pb-40 prose prose-sm h-full"> <article className="m-auto pt-40 pb-40 prose prose-sm h-full">
<PageState.Provider> <PageState.Provider initialState={router.query.id as string}>
<Editor /> <Editor />
</PageState.Provider> </PageState.Provider>
</article> </article>

View File

@ -1,4 +1,4 @@
import { useState } from 'react' import { useEffect, useState } from 'react'
import { createContainer } from 'unstated-next' import { createContainer } from 'unstated-next'
import useFetch from 'use-http' import useFetch from 'use-http'
@ -10,13 +10,13 @@ export interface PageModel {
content?: string content?: string
} }
const usePage = () => { const usePage = (id?: string) => {
const [page, setPage] = useState<PageModel>() const [page, setPage] = useState<PageModel>()
const { get, post } = useFetch(`/api/pages`) const { get, post, response } = useFetch(`/api/pages`)
const getById = async (id: string) => { const getById = async (id: string) => {
console.log(111, id)
const data = await get(id) const data = await get(id)
console.log(111, response.headers)
setPage({ setPage({
id: '0', id: '0',
@ -27,6 +27,12 @@ const usePage = () => {
}) })
} }
useEffect(() => {
if (id) {
getById(id)
}
}, [id])
const savePage = async (data: PageModel) => { const savePage = async (data: PageModel) => {
await post({ await post({
id: data.id, id: data.id,

View File

@ -1,5 +1,24 @@
import { GetServerSideProps } from 'next'
import { getSession } from 'next-auth/client'
import { Layout } from '../components/layout' import { Layout } from '../components/layout'
const IndexPage = () => <Layout></Layout> const IndexPage = () => <Layout></Layout>
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 export default IndexPage

View File

@ -1,7 +1,5 @@
import { Layout } from '../../components/layout' import IndexPage, { getServerSideProps } from '..'
const PagePage = () => { export { getServerSideProps }
return <Layout></Layout>
}
export default PagePage export default IndexPage