Javascript nodejs-如何强制用户在注册后完成配置文件?

Javascript nodejs-如何强制用户在注册后完成配置文件?,javascript,authentication,passport.js,Javascript,Authentication,Passport.js,在用户已使用min信息注册后,如何完全阻止用户表单访问除完整配置文件路由之外的任何路由 完整的个人资料意味着,用户必须添加他的/她的职务和上传id副本,并得到系统管理员的验证,这样他/她就可以正常登录 最好的例子是upwork.com 我已经配置了passport,也许我可以阻止jwt策略 我怎样才能实现它?最佳实践是什么 我用快递 // LOCAL STRATEGY passport.use(new LocalStrategy({ usernameField: 'email' }, asy

在用户已使用min信息注册后,如何完全阻止用户表单访问除完整配置文件路由之外的任何路由

完整的个人资料意味着,用户必须添加他的/她的职务和上传id副本,并得到系统管理员的验证,这样他/她就可以正常登录

最好的例子是upwork.com

我已经配置了passport,也许我可以阻止jwt策略

我怎样才能实现它?最佳实践是什么

我用快递

// LOCAL STRATEGY
passport.use(new LocalStrategy({
  usernameField: 'email'
}, async (email, password, done) => {
  try {
    // Find the user given the email
    const user = await User.findOne({ "local.email": email });

    // If not, handle it
    if (!user) {
      return done(null, false);
    }

    // Check if the password is correct
    const isMatch = await user.isValidPassword(password);

    // If not, handle it
    if (!isMatch) {
      return done(null, false);
    }

    // Otherwise, return the user
    done(null, user);
  } catch(error) {
    done(error, false);
  }
}));

// JSON WEB TOKENS STRATEGY
passport.use(new JwtStrategy({
  jwtFromRequest: cookieExtractor,
  secretOrKey: config.JWT_SECRET,
  passReqToCallback: true
}, async (req, payload, done) => {
  try {
    // Find the user specified in token
    const user = await User.findById(payload.sub);

    // If user doesn't exists, handle it
    if (!user) {
      return done(null, false);
    }

    // Otherwise, return the user
    req.user = user;
    done(null, user);
  } catch(error) {
    done(error, false);
  }
}));

提前感谢您的帮助

您基本上需要基于用户角色的某种ACL

现在您有3个角色->未注册、几乎注册(没有该职位)和完全注册

第一种解决方案-对于每个路由(或路由组),必须指定要使用的授权。我认为目前最简单的方法是保持您的passport实现不变,并添加简单的中间件来检查用户是否已完成配置文件,并基于此拒绝/添加访问

第二个解决方案-您还可以有两个passport实现,一个用于具有完整配置文件的路由(同时检查用户是否具有标题等),另一个用于配置文件完成的端点

最后一种可能性——我不喜欢,因为这不是一个固定的解决方案,从长远来看可能会有一些问题,但它对您当前的解决方案的影响最小,就是检查passport身份验证代码中的路由是什么,如果它与“完整配置文件”端点不同,它还会检查您的配置文件是否已完成

如果您计划在后端拥有更多角色(即一些管理员、某些组的负责人等),请选择第一个或第二个解决方案,或者在node.js中搜索更多关于ACL的内容,因为您无论如何都需要它。
如果您认为没有更多的角色,那么“最后一种可能性”解决方案应该很好。

您基本上需要基于用户角色的某种ACL

现在您有3个角色->未注册、几乎注册(没有该职位)和完全注册

第一种解决方案-对于每个路由(或路由组),必须指定要使用的授权。我认为目前最简单的方法是保持您的passport实现不变,并添加简单的中间件来检查用户是否已完成配置文件,并基于此拒绝/添加访问

第二个解决方案-您还可以有两个passport实现,一个用于具有完整配置文件的路由(同时检查用户是否具有标题等),另一个用于配置文件完成的端点

最后一种可能性——我不喜欢,因为这不是一个固定的解决方案,从长远来看可能会有一些问题,但它对您当前的解决方案的影响最小,就是检查passport身份验证代码中的路由是什么,如果它与“完整配置文件”端点不同,它还会检查您的配置文件是否已完成

如果您计划在后端拥有更多角色(即一些管理员、某些组的负责人等),请选择第一个或第二个解决方案,或者在node.js中搜索更多关于ACL的内容,因为您无论如何都需要它。
如果您认为不再有角色,那么“最后一种可能性”解决方案应该很好。

您使用的是express,因此请编写一个全局中间件函数来检查身份验证状态,并在用户未经身份验证时提供404服务:

// step 1: have a global error handler
// https://expressjs.com/en/guide/error-handling.html

app.use(function(err, req, res, next) {
  res.status(err.status);
  res.render('error', err);
});

// then, your global authentication middleware:
app.use(function(req, res, next) {
  if (/* user authentication checks out */) {
    return next();
  }

  if (req.url === /* your single open route */) {
    return next();
  }

  // user is not authenticated: either we flag a global error, like this:
  next({ error: "user not authenticated", code: 403 });

  // or we redirect the user, because if there's only one route
  // they're allowed to access, just take them to that route.
  res.redirect('route/they/can/access');
});

// And then your actual route bindings go _after_ the above code.

您使用的是express,因此编写一个全局中间件函数来检查身份验证状态,并在用户未经身份验证时提供404服务:

// step 1: have a global error handler
// https://expressjs.com/en/guide/error-handling.html

app.use(function(err, req, res, next) {
  res.status(err.status);
  res.render('error', err);
});

// then, your global authentication middleware:
app.use(function(req, res, next) {
  if (/* user authentication checks out */) {
    return next();
  }

  if (req.url === /* your single open route */) {
    return next();
  }

  // user is not authenticated: either we flag a global error, like this:
  next({ error: "user not authenticated", code: 403 });

  // or we redirect the user, because if there's only one route
  // they're allowed to access, just take them to that route.
  res.redirect('route/they/can/access');
});

// And then your actual route bindings go _after_ the above code.

我可能缺少您实际使用的服务器。你能添加那个信息吗?你是说服务器语言?!在这种情况下,nodejspassport只是一个验证器,可能您也在运行服务器,如hapi或express等?啊,对不起,我使用express,我还会将其添加到问题中。如果您使用express,答案是“编写中间件函数”。我可能缺少您实际使用的服务器。你能添加那个信息吗?你是说服务器语言?!在本例中,nodejspassport只是一个验证器,可能您也在运行服务器,如hapi或express等?啊,对不起,我使用express,我还会将其添加到问题中。如果您使用express,答案是“编写中间件函数”。