MCPcopy Create free account
hub / github.com/coderwhy/coderhub / verifyAuth

Function verifyAuth

src/middleware/auth.middleware.js:39–60  ·  view source on GitHub ↗
(ctx, next)

Source from the content-addressed store, hash-verified

37}
38
39const 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.很多的内容都需要验证权限: 修改/删除动态, 修改/删除评论

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected