feat(app): 添加 API 路由前缀并优化用户信息处理

- 在 API 路由中添加 "/api" 前缀,实现 URL 优化
- 修复用户信息存储逻辑,确保正确信息保存在上下文中
- 更新第三方 Token 验证函数,增加用户名字段
This commit is contained in:
aixianling
2025-02-25 17:49:54 +08:00
parent b895f8f247
commit 448acfc26a
2 changed files with 5 additions and 5 deletions

4
app.js
View File

@@ -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');

View File

@@ -1,3 +1,3 @@
module.exports = token=>{
return {token}
}
module.exports = (token) => {
return { token, username: token };
};