Changeset ee05663 for backend


Ignore:
Timestamp:
09/08/22 17:46:49 (2 years ago)
Author:
Nace Gjorgjievski <nace.gorgievski123@…>
Branches:
master
Children:
55ed171
Parents:
16237c4
Message:

Added Profile Updating

File:
1 edited

Legend:

Unmodified
Added
Removed
  • backend/routes/userRoutes.js

    r16237c4 ree05663  
    33import User from "../models/userModel.js";
    44import bcrypt from "bcryptjs";
    5 import { generateToken } from "../utils.js";
     5import { generateToken, isAuth } from "../utils.js";
    66
    77const userRouter = express.Router();
     
    4949);
    5050
     51userRouter.put(
     52  "/profile",
     53  isAuth,
     54  expressAsyncHandler(async (req, res) => {
     55    const user = await User.findById(req.user._id);
     56    if (user) {
     57      user.name = req.body.name || user.name;
     58      user.contact = req.body.contact || user.contact;
     59      user.email = req.body.email || user.email;
     60      if (req.body.password) {
     61        user.password = bcrypt.hashSync(req.body.password, 8);
     62      }
     63
     64      const updatedUser = await user.save();
     65      res.send({
     66        _id: updatedUser._id,
     67        name: updatedUser.name,
     68        contact: updatedUser.contact,
     69        email: updatedUser.email,
     70        isAdmin: updatedUser.isAdmin,
     71        token: generateToken(updatedUser),
     72      });
     73    } else {
     74      res.status(404).send({ message: "User Not Found" });
     75    }
     76  })
     77);
     78
    5179export default userRouter;
Note: See TracChangeset for help on using the changeset viewer.