Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MD-ASHARUDDIN-90 committed May 12, 2024
2 parents 357f85f + 923002d commit ad317d2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
14 changes: 13 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ const port = process.env.PORT || 8080;

//middleware
useMorgan(app); //logs requests
app.use(cors());
const allowedOrigins = [
process.env.CLIENT_DOMAIN || "https://chit-chat-client-fawn.vercel.app",
];
app.use(
cors({
origin: (origin, callback) => {
if (allowedOrigins.includes(origin)) {
return callback(null, true);
}
return callback(new Error("Not allowed by CORS"));
},
}),
);
app.use(express.json());
app.use(express.urlencoded({ extended: true, limit: "16kb" }));
app.use(express.static("public"));
Expand Down
33 changes: 32 additions & 1 deletion src/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Users from "../models/userModel.js";
import { uploadToCloudinary } from "../utility/cloudinary.js";
import {
comparePassword,
createHashedPassword,
} from "../utility/hashedPassword.js";

async function getUserData(req, res) {
try {
Expand Down Expand Up @@ -34,4 +38,31 @@ async function updateUserData(req, res) {
res.status(500).json({ message: "Internal server error" });
}
}
export { getUserData, updateUserData };

async function updateUserPassword(req, res) {
try {
const { id } = req.user;
const user = await Users.findById(id);

const { currentPassword, newPassword, confirmNewPassword } = req.body;

const match = await comparePassword(currentPassword, user.password);
if (!match) {
return res.status(401).json({ message: "Invalid current password" });
}

if (newPassword !== confirmNewPassword) {
return res.status(400).json({
message: "New password and confirm new password do not match",
});
}

user.password = await createHashedPassword(newPassword);
await user.save();
res.status(200).json({ message: "Password Updated Successfully" });
} catch (error) {
res.status(500).json({ message: "Internal server error" });
}
}

export { getUserData, updateUserData, updateUserPassword };
7 changes: 6 additions & 1 deletion src/routes/userRoutes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import express from "express";
import { getUserData, updateUserData } from "../controllers/userController.js";
import {
getUserData,
updateUserData,
updateUserPassword,
} from "../controllers/userController.js";
import { uploadMiddleware } from "../middleware/multerMiddleware.js";

const router = express.Router();

router.get("/", getUserData);
router.put("/update", uploadMiddleware, updateUserData);
router.put("/update-password", updateUserPassword);

export default router;

0 comments on commit ad317d2

Please sign in to comment.