0

i tried req.login which is a passport method which i think its used when we sign up only but it didnt work and i see this solution Passport-Local-Mongoose – When I Update A Record's Username, I'm Logged Out, Why? but it didn`t work and

 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);
  });
}

);

1 Answers1

0

i found the solution the problem happens when you change username and passport is saving your username already on the session {user: ''} so when u change with new username we don`t update passport user with new username so try this

 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
      )
    );
  }