From 448acfc26a0935fd0bd6a0b5c8e4ec6136918dec Mon Sep 17 00:00:00 2001 From: aixianling Date: Tue, 25 Feb 2025 17:49:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=20=E6=B7=BB=E5=8A=A0=20API=20?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E5=89=8D=E7=BC=80=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 API 路由中添加 "/api" 前缀,实现 URL 优化 - 修复用户信息存储逻辑,确保正确信息保存在上下文中 - 更新第三方 Token 验证函数,增加用户名字段 --- app.js | 4 ++-- auth/verifyThirdPartyToken.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index 0d5a4f5..0f130b4 100644 --- a/app.js +++ b/app.js @@ -18,7 +18,7 @@ const loadAPIRoutes = () => { files.forEach((file) => { if (file.endsWith(".js") && file !== "index.js") { - const routePath = `/${file.replace(".js", "")}`; + const routePath = `/api/${file.replace(".js", "")}`; const handler = require(path.join(apiDir, file)); router.post(routePath, async (ctx) => { @@ -42,7 +42,7 @@ app.use(async (ctx, next) => { // 这里假设第三方Token可以通过某种方式验证并转换为JWT Token const decoded = verifyThirdPartyToken(thirdPartyToken); // 假设有一个验证函数 const jwtToken = jwt.sign(decoded, process.env.JWT_SECRET, { expiresIn: "1h" }); - ctx.state.user = user; // 将用户信息存储在ctx.state中 + ctx.state.user = decoded; // 将用户信息存储在ctx.state中 ctx.headers.authorization = `Bearer ${jwtToken}`; // 替换为JWT Token } catch (err) { ctx.throw(401, 'Invalid third-party token'); diff --git a/auth/verifyThirdPartyToken.js b/auth/verifyThirdPartyToken.js index 018e478..5853ddc 100644 --- a/auth/verifyThirdPartyToken.js +++ b/auth/verifyThirdPartyToken.js @@ -1,3 +1,3 @@ -module.exports = token=>{ - return {token} -} \ No newline at end of file +module.exports = (token) => { + return { token, username: token }; +};