Started update user route
This commit is contained in:
1 parent
5fa73a1f17
commit
d84a0f9c87
7 files changed
+125
-46
No files matched your search
@@ -1,5 +1,10 @@
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const { registerValidation, loginValidation } = require("../validation/joi");
|
const {
|
||||||
|
registerValidation,
|
||||||
|
loginValidation,
|
||||||
|
editUserParamValidation,
|
||||||
|
editUserBodyValidation,
|
||||||
|
} = require("../validation/joi");
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
var jwt = require("jsonwebtoken");
|
var jwt = require("jsonwebtoken");
|
||||||
@@ -107,4 +112,37 @@ const loginController = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { registerController, loginController };
|
const userEditController = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
await editUserParamValidation.validateAsync(req.params);
|
||||||
|
await editUserBodyValidation.validateAsync(req.body);
|
||||||
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.params.userId !== req.user._id.toString()) {
|
||||||
|
const error = new Error("Unauthorized access");
|
||||||
|
error.status = 401;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Find and update user
|
||||||
|
try {
|
||||||
|
const updatedUser = await req.db.updateUser(req, res);
|
||||||
|
res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "User updated", data: updatedUser });
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = { registerController, loginController, userEditController };
|
||||||
@@ -55,6 +55,31 @@ const getUserByEmail = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a user by ID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<UserModel>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const updateUser = async (req, res) => {
|
||||||
|
const candidateUserId = req.params.userId;
|
||||||
|
const candidateUser = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const updatedUser = await user.findByIdAndUpdate(
|
||||||
|
candidateUserId,
|
||||||
|
candidateUser,
|
||||||
|
{ new: true } // Returns updated user instead of pre-update user
|
||||||
|
);
|
||||||
|
return updatedUser;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all monitors
|
* Get all monitors
|
||||||
* @async
|
* @async
|
||||||
@@ -173,6 +198,7 @@ module.exports = {
|
|||||||
connect,
|
connect,
|
||||||
insertUser,
|
insertUser,
|
||||||
getUserByEmail,
|
getUserByEmail,
|
||||||
|
updateUser,
|
||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
getMonitorsByUserId,
|
getMonitorsByUserId,
|
||||||
|
|||||||
+6
-6
@@ -63,12 +63,6 @@ app.use((req, res, next) => {
|
|||||||
app.use("/api/v1/auth", authRouter);
|
app.use("/api/v1/auth", authRouter);
|
||||||
app.use("/api/v1/monitors", verifyJWT, monitorRouter);
|
app.use("/api/v1/monitors", verifyJWT, monitorRouter);
|
||||||
|
|
||||||
/**
|
|
||||||
* Error handler middleware
|
|
||||||
* MUST be called after all routes
|
|
||||||
*/
|
|
||||||
app.use(handleErrors);
|
|
||||||
|
|
||||||
// Testing email service
|
// Testing email service
|
||||||
// app.use('/sendEmail', async (req, res) => {
|
// app.use('/sendEmail', async (req, res) => {
|
||||||
// const response = sendEmail(['veysel.boybay@bluewavelabs.ca'], 'Testing email service', '<h1>Testing Bluewavelabs</h1>');
|
// const response = sendEmail(['veysel.boybay@bluewavelabs.ca'], 'Testing email service', '<h1>Testing Bluewavelabs</h1>');
|
||||||
@@ -86,4 +80,10 @@ app.use("/api/v1/healthy", (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error handler middleware
|
||||||
|
* Should be called last
|
||||||
|
*/
|
||||||
|
app.use(handleErrors);
|
||||||
|
|
||||||
connectDbAndRunServer(app, db);
|
connectDbAndRunServer(app, db);
|
||||||
@@ -19,6 +19,7 @@ const verifyOwnership = (Model, paramName) => {
|
|||||||
|
|
||||||
// If the userID does not match the document's userID, return a 403 error
|
// If the userID does not match the document's userID, return a 403 error
|
||||||
if (userId.toString() !== doc.userId.toString()) {
|
if (userId.toString() !== doc.userId.toString()) {
|
||||||
|
console.log("boom");
|
||||||
const error = new Error("Unauthorized access");
|
const error = new Error("Unauthorized access");
|
||||||
error.status = 403;
|
error.status = 403;
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
+34
-37
@@ -1,44 +1,41 @@
|
|||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
const bcrypt = require("bcrypt");
|
const bcrypt = require("bcrypt");
|
||||||
|
|
||||||
const UserSchema = mongoose.Schema({
|
const UserSchema = mongoose.Schema(
|
||||||
firstname: {
|
{
|
||||||
type: String,
|
firstname: {
|
||||||
required: true,
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
lastname: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
email: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
unique: true,
|
||||||
|
},
|
||||||
|
password: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
profilePicUrl: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
isActive: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true,
|
||||||
|
},
|
||||||
|
isVerified: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
lastname: {
|
{
|
||||||
type: String,
|
timestamps: true,
|
||||||
required: true,
|
}
|
||||||
},
|
);
|
||||||
email: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
unique: true,
|
|
||||||
},
|
|
||||||
password: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
profilePicUrl: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
isActive: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
isVerified: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false,
|
|
||||||
},
|
|
||||||
updated_at: {
|
|
||||||
type: Date,
|
|
||||||
default: Date.now,
|
|
||||||
},
|
|
||||||
created_at: {
|
|
||||||
type: Date,
|
|
||||||
default: Date.now,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
UserSchema.pre("save", async function (next) {
|
UserSchema.pre("save", async function (next) {
|
||||||
if (!this.isModified("password")) {
|
if (!this.isModified("password")) {
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
const router = require("express").Router();
|
const router = require("express").Router();
|
||||||
|
const user = require("../models/user");
|
||||||
|
const { verifyJWT } = require("../middleware/verifyJWT");
|
||||||
|
const { verifyOwnership } = require("../middleware/verifyOwnership");
|
||||||
|
|
||||||
const {
|
const {
|
||||||
registerController,
|
registerController,
|
||||||
loginController,
|
loginController,
|
||||||
|
userEditController,
|
||||||
} = require("../controllers/authController");
|
} = require("../controllers/authController");
|
||||||
|
|
||||||
router.post("/register", registerController);
|
router.post("/register", registerController);
|
||||||
router.post("/login", loginController);
|
router.post("/login", loginController);
|
||||||
|
router.post("/user/:userId", verifyJWT, userEditController);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
const joi = require("joi");
|
const joi = require("joi");
|
||||||
const user = require("../models/user");
|
|
||||||
|
|
||||||
const loginValidation = joi.object({
|
const loginValidation = joi.object({
|
||||||
email: joi.string().email().required(),
|
email: joi.string().email().required(),
|
||||||
@@ -30,10 +29,22 @@ const monitorValidation = joi.object({
|
|||||||
interval: joi.number(),
|
interval: joi.number(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const editUserParamValidation = joi.object({
|
||||||
|
userId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const editUserBodyValidation = joi.object({
|
||||||
|
firstname: joi.string(),
|
||||||
|
lastname: joi.string(),
|
||||||
|
profilePicUrl: joi.string(),
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
loginValidation,
|
loginValidation,
|
||||||
registerValidation,
|
registerValidation,
|
||||||
getMonitorByIdValidation,
|
getMonitorByIdValidation,
|
||||||
getMonitorsByUserIdValidation,
|
getMonitorsByUserIdValidation,
|
||||||
monitorValidation,
|
monitorValidation,
|
||||||
|
editUserParamValidation,
|
||||||
|
editUserBodyValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user