Ver Fonte

中间件默认不使用

lrf há 3 anos atrás
pai
commit
bd7522c683
2 ficheiros alterados com 23 adições e 18 exclusões
  1. 11 8
      app/middleware/check-token.js
  2. 12 10
      app/middleware/password.js

+ 11 - 8
app/middleware/check-token.js

@@ -29,14 +29,17 @@ const checkJwt = (token, secret) => {
 };
 
 
-module.exports = options => {
+module.exports = ({ enable = false }) => {
   return async function checkToken(ctx, next) {
-    // token处理
-    const token = _.get(ctx.request, 'header.authorization');
-    if (token) {
-      const r = checkJwt(token, ctx.app.config.jwt.secret);
-      ctx.user = r;
-    }
-    await next();
+    if (enable) {
+      // token处理
+      const token = _.get(ctx.request, 'header.authorization');
+      if (token) {
+        const r = checkJwt(token, ctx.app.config.jwt.secret);
+        ctx.user = r;
+      }
+      await next();
+    } else await next();
+
   };
 };

+ 12 - 10
app/middleware/password.js

@@ -1,14 +1,16 @@
 'use strict';
-module.exports = options => {
+module.exports = ({ enable = false }) => {
   return async function password(ctx, next) {
-    // mongodb中secret转换为密码类型
-    const method = ctx.request.method;
-    const isLoginEnd = ctx.request.url.includes('login');
-    const passwordNoDeal = ctx.request.headers.passwordnodeal;
-    if (method !== 'GET' && !isLoginEnd && !passwordNoDeal) {
-      const body = ctx.request.body;
-      if (body && body.password) body.password = { secret: body.password };
-    }
-    await next();
+    if (enable) {
+      // mongodb中secret转换为密码类型
+      const method = ctx.request.method;
+      const isLoginEnd = ctx.request.url.includes('login');
+      const passwordNoDeal = ctx.request.headers.passwordnodeal;
+      if (method !== 'GET' && !isLoginEnd && !passwordNoDeal) {
+        const body = ctx.request.body;
+        if (body && body.password) body.password = { secret: body.password };
+      }
+      await next();
+    } else await next();
   };
 };