From c862ff3e8d78fe7d2e2ada1f7ed3d2d83d7e3e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E7=90=AA=E6=96=8C?= Date: Mon, 5 Jul 2021 15:13:50 +0800 Subject: [PATCH] fix: default skin --- public/theme.js | 4 ++++ server/controllers/auth.ts | 5 +---- server/middleware/auth.ts | 9 +++++++-- src/components/Header/Base.tsx | 6 ------ src/pages/_document.tsx | 2 ++ 5 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 public/theme.js diff --git a/public/theme.js b/public/theme.js new file mode 100644 index 0000000..ce5256c --- /dev/null +++ b/public/theme.js @@ -0,0 +1,4 @@ +var skin = localStorage.getItem('skin'); +if (skin) { + document.documentElement.classList.add(skin); +} diff --git a/server/controllers/auth.ts b/server/controllers/auth.ts index 5a09f35..c8268f0 100644 --- a/server/controllers/auth.ts +++ b/server/controllers/auth.ts @@ -54,10 +54,7 @@ authRouter.post('/signin', async (req, res) => { //检查密码是否匹配 const pwdMatchFlag = bcrypt.compareSync(password, user.password); if (pwdMatchFlag) { - const token = jwt.sign( - { ...user, password: undefined }, - process.env.JWT_SECRET - ); + const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET); res.cookie('token', token, { httpOnly: true }); res.json({ token: token, diff --git a/server/middleware/auth.ts b/server/middleware/auth.ts index 8665d72..7acac64 100644 --- a/server/middleware/auth.ts +++ b/server/middleware/auth.ts @@ -4,7 +4,7 @@ import jwt from 'jsonwebtoken'; import prisma from '../prisma'; export interface Req extends Request { - user: User; + user: Omit; } export async function protect( @@ -21,9 +21,14 @@ export async function protect( try { const token = req.cookies.token; - const decoded = jwt.verify(token, process.env.JWT_SECRET) as User; + const decoded = jwt.verify(token, process.env.JWT_SECRET) as { id: number }; if (decoded.id) { const user = await prisma.user.findUnique({ + select: { + id: true, + name: true, + email: true, + }, where: { id: decoded.id, }, diff --git a/src/components/Header/Base.tsx b/src/components/Header/Base.tsx index c32253e..2fc34a0 100644 --- a/src/components/Header/Base.tsx +++ b/src/components/Header/Base.tsx @@ -7,7 +7,6 @@ import Icon from '../Icon'; interface Props { children: ReactElement; } - export default function Header({ children }: Props): ReactElement { const { user } = useMe(); const [current, setCurrent] = useState(''); @@ -22,11 +21,6 @@ export default function Header({ children }: Props): ReactElement { const skin = localStorage.getItem('skin'); if (skin) { setCurrent(skin); - document.documentElement.classList.add(skin); - } else { - document.documentElement.classList.add('theme-light'); - localStorage.setItem('skin', 'theme-light'); - setCurrent(skin); } }, []); return ( diff --git a/src/pages/_document.tsx b/src/pages/_document.tsx index 593144f..7803624 100644 --- a/src/pages/_document.tsx +++ b/src/pages/_document.tsx @@ -1,3 +1,4 @@ +/* eslint-disable @next/next/no-sync-scripts */ import Document, { Html, Head, Main, NextScript } from 'next/document'; import { description, keywords } from '../config'; @@ -14,6 +15,7 @@ class MyDocument extends Document { +