password.js 576 B

12345678910111213141516
  1. 'use strict';
  2. module.exports = ({ enable = false }) => {
  3. return async function password(ctx, next) {
  4. if (enable) {
  5. // mongodb中secret转换为密码类型
  6. const method = ctx.request.method;
  7. const isLoginEnd = ctx.request.url.includes('login');
  8. const passwordNoDeal = ctx.request.headers.passwordnodeal;
  9. if (method !== 'GET' && !isLoginEnd && !passwordNoDeal) {
  10. const body = ctx.request.body;
  11. if (body && body.password) body.password = { secret: body.password };
  12. }
  13. await next();
  14. } else await next();
  15. };
  16. };