【问题标题】:automatic logout after change any user info passport-local-mongoose更改任何用户信息后自动注销passport-local-mongoose
【发布时间】:2021-08-21 17:51:06
【问题描述】:

我尝试了 req.login,这是一种护照方法,我认为它s used when we sign up only but it didnt 可以工作,我看到了这个解决方案Passport-Local-Mongoose – When I Update A Record's Username, I'm Logged Out, Why?,但它没有用,并且

 async (req, res, next) => {
  const {id} = req.params;
  if (req.file) {
    const {path, filename} = req.file;

    const foundUser = await User.findByIdAndUpdate(id, {
      profileImage: {profileUrl: path, filename},
    });
  }
  const foundUser = await User.findByIdAndUpdate(id, req.body.user);
  req.logIn(foundUser, function (err) {
    if (err) {
      return next(err);
    }
    return res.redirect("/user/" + foundUser.username);
  });
}

);

【问题讨论】:

    标签: node.js express passport-local passport-local-mongoose


    【解决方案1】:

    我找到了解决方案,当您更改用户名时会发生问题,并且护照已在会话 {user: ''} 中保存您的用户名,因此当您使用新用户名更改时,我们不会使用新用户名更新护照用户 所以试试这个

     try {
        const foundUser = await User.findByIdAndUpdate(id, req.body.user);
        req.session.passport.user = req.body.user.username;
        return res.redirect("/users/" + id);
      } catch (e) {
        const duplicated = Object.keys(e.keyPattern);
        return next(
          new AppError(
            `this ${duplicated} already in use, you can enter another ${duplicated} `,
            406
          )
        );
      }
     
    

    【讨论】:

      猜你喜欢
      • 2013-07-23
      • 2014-07-21
      • 2023-03-30
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2018-07-27
      • 1970-01-01
      相关资源
      最近更新 更多