Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
feat: 🎸 增加全局路由鉴权 close:#166
Browse files Browse the repository at this point in the history
Closes: #166
  • Loading branch information
danni-cool committed Feb 27, 2024
1 parent 6447c45 commit 875dfb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
12 changes: 0 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ const registerRoute = require('./src/route')
const bot = wechatBotInit()
const app = new Hono()

/**
* @param {import('hono').Context} ctx
* @param {import('hono').Next} next
*/
const attachData = (ctx, next) => {
ctx.bot = bot
return next()
}

app.use('*', attachData)

// 注册webhook
registerRoute({ app, bot })

serve({
Expand Down
14 changes: 14 additions & 0 deletions src/route/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
const Middleware = require('../middleware/index')
/**
* 注册路由
* @param {Object} param
* @param {import('hono').Hono} param.app
* @param {import('wechaty').Wechaty} param.bot
*/
module.exports = function registerRoute({ app, bot }) {
/**
* @param {import('hono').Context} ctx
* @param {import('hono').Next} next
*/
const attachData = (ctx, next) => {
ctx.bot = bot
return next()
}
// 挂载wecahty实例到全局路由
app.use('*', attachData)
// 全局鉴权
app.use(Middleware.verifyToken)

require('./msg')({ app, bot })
require('./login')({ app, bot })
}
4 changes: 0 additions & 4 deletions src/route/login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const Service = require('../service')
const Utils = require('../utils')
const Middleware = require('../middleware')
const { TextMsg } = require('../utils/msg.js')

/**
Expand Down Expand Up @@ -87,8 +86,6 @@ module.exports = function registerLoginCheck({ app, bot }) {

app.get(
'/login',
Middleware.verifyToken,

/** @param {import('hono').Context} c */
async (c) => {
// 登录成功的话,返回登录信息
Expand All @@ -105,7 +102,6 @@ module.exports = function registerLoginCheck({ app, bot }) {

app.get(
'/healthz',
Middleware.verifyToken,
/** @param {import('hono').Context} c */
async (c) => {
// 登录成功的话,返回登录信息
Expand Down

0 comments on commit 875dfb3

Please sign in to comment.