(ctx, next)
| 37 | } |
| 38 | |
| 39 | const verifyAuth = async (ctx, next) => { |
| 40 | console.log("验证授权的middleware~"); |
| 41 | // 1.获取token |
| 42 | const authorization = ctx.headers.authorization; |
| 43 | if (!authorization) { |
| 44 | const error = new Error(errorTypes.UNAUTHORIZATION); |
| 45 | return ctx.app.emit('error', error, ctx); |
| 46 | } |
| 47 | const token = authorization.replace('Bearer ', ''); |
| 48 | |
| 49 | // 2.验证token(id/name/iat/exp) |
| 50 | try { |
| 51 | const result = jwt.verify(token, PUBLIC_KEY, { |
| 52 | algorithms: ["RS256"] |
| 53 | }); |
| 54 | ctx.user = result; |
| 55 | await next(); |
| 56 | } catch (err) { |
| 57 | const error = new Error(errorTypes.UNAUTHORIZATION); |
| 58 | ctx.app.emit('error', error, ctx); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * 1.很多的内容都需要验证权限: 修改/删除动态, 修改/删除评论 |
nothing calls this directly
no outgoing calls
no test coverage detected