userController.js
export const postEditProfile = async (req, res) => {
const { body: { name, email }, file } = req;
try{
const user = await User.findByIdAndUpdate(req.user._id, {
name,
email,
avatarUrl: file ? file.path : req.user.avatarUrl
});
user.save();
res.redirect(routes.me);
}catch(error){
res.render("editProfile", { pageTitle: "Edit Profile" });
}
};
middlewares.js
...
const multerAvatar = multer({ dest: "uploads/avatars/" });
...
export const uploadAvator = multerAvatar.single("avatar");
userRouter.js
...
import { onlyPrivate, privatePublic, uploadAvator } from "../middlewares";
...
userRouter.post(routes.editProfile, onlyPrivate, uploadAvator, postEditProfile);
editProfile.js
form(action=`/users${routes.editProfile}`, method="post", enctype="multipart/form-data")
.fileUpload
label(for="avatar") Avatar
input(type="file", id="avatar", name="avatar", accept="image/*")
input(type="text", placeholder="Name", name="name", value=loggedUser.name)
input(type="email", placeholder="Email", name="email", value=loggedUser.email)
input(type="submit", value="Update Profile")
191115 버그 : 이메일 수정시 로그아웃이 되면서 정보를 잃음