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 { +