Added backend folder structure.
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# Environment files
|
||||||
|
.env
|
||||||
|
.env.*.local
|
||||||
|
|
||||||
|
# Build outputs
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
|
||||||
|
# Static files
|
||||||
|
public/
|
||||||
|
out/
|
||||||
|
|
||||||
|
# Test files
|
||||||
|
coverage/
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# Database files
|
||||||
|
*.sqlite3
|
||||||
|
*.db
|
||||||
|
*.db-wal
|
||||||
|
*.db-shm
|
||||||
|
|
||||||
|
# Temporary files
|
||||||
|
*.tmp
|
||||||
|
*.temp
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
|
||||||
|
# IDE and editor files
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
*.iml
|
||||||
|
|
||||||
|
# System files
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
|
|
||||||
|
# Other
|
||||||
|
*.log.*
|
||||||
@@ -0,0 +1,464 @@
|
|||||||
|
const catchAsyncErrors = require("../Middlewares/catchAsyncErrors.js");
|
||||||
|
const User = require("../Models/user.model.js");
|
||||||
|
const { uploadOnCloudinary } = require("../Utils/cloudinary.js");
|
||||||
|
const sendEmail = require("../Utils/sendmail.js");
|
||||||
|
const crypto = require("crypto");
|
||||||
|
const jwt = require("jsonwebtoken");
|
||||||
|
|
||||||
|
// Register or Sign up new User -- Done
|
||||||
|
const registerUser = catchAsyncErrors(async (req, res) => {
|
||||||
|
const { name, email, password, role } = req.body;
|
||||||
|
|
||||||
|
const user = await User.create({
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
role,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "User not created something went wrong.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "User is registered successfully",
|
||||||
|
data: user,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Login user in our web app -- Done
|
||||||
|
const loginUser = catchAsyncErrors(async (req, res) => {
|
||||||
|
const { email, password } = req.body;
|
||||||
|
|
||||||
|
const user = await User.findOne({ email });
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "User not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkUser = await user.isPasswordCorrect(password);
|
||||||
|
|
||||||
|
if (!checkUser) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Password is incorrect",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = await user.generateRefreshToken();
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "token not created something went wrong.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
user.password = null;
|
||||||
|
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.cookie(process.env.TOKEN_NAME, token, {
|
||||||
|
path: "/",
|
||||||
|
sameSite: "None",
|
||||||
|
secure: process.env.NODE_ENV === "production",
|
||||||
|
httpOnly: true,
|
||||||
|
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
|
||||||
|
})
|
||||||
|
.json({
|
||||||
|
success: true,
|
||||||
|
message: "User is successfully logged in.",
|
||||||
|
data: user,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Logout user in our web app -- Done
|
||||||
|
const logoutUser = catchAsyncErrors(async (req, res) => {
|
||||||
|
return res
|
||||||
|
.clearCookie(process.env.TOKEN_NAME, {
|
||||||
|
path: "/",
|
||||||
|
sameSite: "None",
|
||||||
|
secure: process.env.NODE_ENV === "production",
|
||||||
|
httpOnly: true,
|
||||||
|
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
|
||||||
|
})
|
||||||
|
.status(201)
|
||||||
|
.json({
|
||||||
|
success: true,
|
||||||
|
message: "User is logged out successfully",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// -- DONE
|
||||||
|
const intializeUser = catchAsyncErrors(async (req, res) => {
|
||||||
|
const tokenValue = req.cookies[process.env.TOKEN_NAME];
|
||||||
|
|
||||||
|
// console.log("I am the one who is doing this : ", tokenValue);
|
||||||
|
|
||||||
|
if (!tokenValue) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "User is not logged in.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const payload = await jwt.verify(
|
||||||
|
tokenValue,
|
||||||
|
process.env.REFRESH_TOKEN_SECRET
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!payload) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "Something went wrong",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await User.findById(payload._id).select("-password");
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "User data get successfully",
|
||||||
|
data: user,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "Something went wrong",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update user deatails -- ADMIN
|
||||||
|
const updateUserDetails = catchAsyncErrors(async (req, res) => {
|
||||||
|
const user = await User.findById(req.params.id);
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: true,
|
||||||
|
message: "User not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const { name, email } = req.body;
|
||||||
|
|
||||||
|
const updateUser = await User.findByIdAndUpdate(req.params.id, {
|
||||||
|
$set: {
|
||||||
|
name: name,
|
||||||
|
email: email,
|
||||||
|
},
|
||||||
|
}).select("-password");
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "User is updated successfully",
|
||||||
|
data: updateUser,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// forget password -- Done
|
||||||
|
const forgetPassword = catchAsyncErrors(async (req, res) => {
|
||||||
|
const { email } = req.body;
|
||||||
|
const user = await User.findOne({ email });
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "User not found ",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// get reset password
|
||||||
|
|
||||||
|
const resetToken = await user.getResetPassword();
|
||||||
|
|
||||||
|
await user.save({ validateBeforeSave: false });
|
||||||
|
|
||||||
|
/*const resetPasswordUrl = `${req.protocol}://${req.get(
|
||||||
|
"host"
|
||||||
|
)}/api/v1/password/reset/${resetToken}`;*/
|
||||||
|
|
||||||
|
const resetPasswordUrl = `${process.env.FRONTEND_URI}/user/api/v1/password/reset/${resetToken}`;
|
||||||
|
|
||||||
|
const message = `Your password token is :-\n\n${resetPasswordUrl}\n\nIf you are not requested this email then please ingore this mail.`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sendEmail({
|
||||||
|
email: user.email,
|
||||||
|
subject: "MentorFlux password recovery",
|
||||||
|
message: message,
|
||||||
|
});
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: `Email sent to ${email} successfully`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
user.resetPasswordToken = undefined;
|
||||||
|
user.resetPasswordExpiry = undefined;
|
||||||
|
await user.save({ validateBeforeSave: false });
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Something went wrong ",
|
||||||
|
error: error,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// reset users password -- DONE
|
||||||
|
const resetPassword = catchAsyncErrors(async (req, res) => {
|
||||||
|
const token = req.params.token;
|
||||||
|
|
||||||
|
const { password, confirmPassword } = req.body;
|
||||||
|
|
||||||
|
//console.log("My password is :", password);
|
||||||
|
//console.log("My confirmPassword is :", confirmPassword);
|
||||||
|
//console.log("My token is :", token);
|
||||||
|
const resetPasswordToken = await crypto
|
||||||
|
.createHash("sha256")
|
||||||
|
.update(token)
|
||||||
|
.digest("hex");
|
||||||
|
|
||||||
|
const user = await User.findOne({
|
||||||
|
resetPasswordToken,
|
||||||
|
resetPasswordExpiry: { $gte: Date.now() },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(401).json({
|
||||||
|
success: false,
|
||||||
|
message: "Reset Password token is invalid or has been expired",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (password !== confirmPassword) {
|
||||||
|
return res.status(401).json({
|
||||||
|
success: false,
|
||||||
|
message: "Please enter password and confirm password",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
user.password = password;
|
||||||
|
user.resetPasswordToken = undefined;
|
||||||
|
user.resetPasswordExpiry = undefined;
|
||||||
|
|
||||||
|
//console.log("To check the user ", user);
|
||||||
|
|
||||||
|
await user.save();
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "Password changed successfully",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// get user personal details
|
||||||
|
const getUserDetails = catchAsyncErrors(async (req, res) => {
|
||||||
|
const user = await User.findById(req.user._id);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Something went wrong ",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "User details are fetched successfully",
|
||||||
|
data: user,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update users password
|
||||||
|
const updatePassword = catchAsyncErrors(async (req, res) => {
|
||||||
|
const { password, oldPassword, confirmPassword } = req.body;
|
||||||
|
|
||||||
|
const user = await User.findById(req.user._id);
|
||||||
|
|
||||||
|
const isPasswordMatched = await user.isPasswordCorrect(oldPassword);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "User not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isPasswordMatched) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Old password is incorrect.Please enter correct password ",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (password !== confirmPassword) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Password and Confirm password should be same.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
user.password = password;
|
||||||
|
await user.save({ validateBeforeSave: false });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "Password upadated successfully",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// update personal details
|
||||||
|
const updatePersonalDetails = catchAsyncErrors(async (req, res) => {
|
||||||
|
const { name, email } = req.body;
|
||||||
|
const user = await User.findByIdAndUpdate(req.user._id, {
|
||||||
|
$set: {
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Something went wrong",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "User details updated successfully",
|
||||||
|
data: user,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get all users details -- ADMIN
|
||||||
|
const getAllusersDetail = catchAsyncErrors(async (req, res) => {
|
||||||
|
const users = await find();
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "All user fetch successfully",
|
||||||
|
data: users,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// get single user details
|
||||||
|
const getSingaluserDetail = catchAsyncErrors(async (req, res) => {
|
||||||
|
const user = await User.findById(req.params.id);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "User not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// upadate user Role -- ADMIN
|
||||||
|
const updateUserRole = catchAsyncErrors(async (req, res) => {
|
||||||
|
const { name, email, role } = req.body;
|
||||||
|
const user = await User.findByIdAndUpdate(req.params.id, {
|
||||||
|
$set: {
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
role,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Something went wrong",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "User Role updated successfully",
|
||||||
|
data: user,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Delete user -- ADMIN
|
||||||
|
const DeleteUser = catchAsyncErrors(async (req, res) => {
|
||||||
|
const user = await User.findByIdAndDelete(req.params.id);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "User does not exist",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "User deleted successfully",
|
||||||
|
data: user,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// update avatar -- user
|
||||||
|
const updateAvatar = catchAsyncErrors(async (req, res) => {
|
||||||
|
//console.log("Our file is : ", req.file.path);
|
||||||
|
|
||||||
|
if (!req.file.path) {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Avatar not uploaded on cloudinary.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const avatarUrl = await uploadOnCloudinary(req.file.path);
|
||||||
|
|
||||||
|
if (!avatarUrl) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Avatar not uploaded on cloudinary.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//console.log("Avatar url is : ", avatarUrl);
|
||||||
|
|
||||||
|
//console.log("our user is : ", req.user);
|
||||||
|
|
||||||
|
const user = await User.findByIdAndUpdate(req.user._id, {
|
||||||
|
$set: {
|
||||||
|
avatar: avatarUrl,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "User not found.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "Avatar updated successfully.",
|
||||||
|
data: user,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
registerUser,
|
||||||
|
loginUser,
|
||||||
|
logoutUser,
|
||||||
|
updateUserDetails,
|
||||||
|
forgetPassword,
|
||||||
|
resetPassword,
|
||||||
|
getUserDetails,
|
||||||
|
updatePassword,
|
||||||
|
updatePersonalDetails,
|
||||||
|
updateUserRole,
|
||||||
|
DeleteUser,
|
||||||
|
intializeUser,
|
||||||
|
updateAvatar,
|
||||||
|
};
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
const mongoose = require("mongoose");
|
||||||
|
const catchAsyncErrors = require("../Middlewares/catchAsyncErrors.js");
|
||||||
|
|
||||||
|
const DB_connect = catchAsyncErrors(async () => {
|
||||||
|
try {
|
||||||
|
const connectionInstance = await mongoose.connect(
|
||||||
|
`${process.env.MONGODB_URI}/${process.env.DATABASE_NAME}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!connectionInstance) {
|
||||||
|
console.log("MongoDB connection failed");
|
||||||
|
}
|
||||||
|
console.log(
|
||||||
|
"MongoDB connected Successfully on server : " +
|
||||||
|
connectionInstance.connection.host
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.log("MongoDB connection failed due to some error :", error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
module.exports = DB_connect;
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
const jwt = require("jsonwebtoken");
|
||||||
|
const User = require("../Models/user.model.js");
|
||||||
|
|
||||||
|
function checkAuthenticated() {
|
||||||
|
return async (req, res, next) => {
|
||||||
|
const tokenValue = req.cookies[process.env.TOKEN_NAME];
|
||||||
|
|
||||||
|
// console.log("I am called",tokenValue);
|
||||||
|
if (!tokenValue) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const payload = await jwt.verify(
|
||||||
|
tokenValue,
|
||||||
|
process.env.REFRESH_TOKEN_SECRET
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!payload) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
req.user = await User.findById(payload._id).select("-password");
|
||||||
|
|
||||||
|
return next();
|
||||||
|
} catch (error) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
return next();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function authorizeRoles(...roles) {
|
||||||
|
return async (req, res, next) => {
|
||||||
|
if (!roles.includes(req.user.role)) {
|
||||||
|
return res.status(401).json({
|
||||||
|
success: false,
|
||||||
|
message: "You are unauthorised to access this resource",
|
||||||
|
});
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
return next();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { checkAuthenticated, authorizeRoles };
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (theFunc) => (req, res, next) => {
|
||||||
|
Promise.resolve(theFunc(req, res, next)).catch(next);
|
||||||
|
};
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
const multer = require("multer");
|
||||||
|
|
||||||
|
const storage = multer.diskStorage({
|
||||||
|
|
||||||
|
destination: function (req, file, cb) {
|
||||||
|
cb(null, "./public/images");
|
||||||
|
},
|
||||||
|
|
||||||
|
filename: function (req, file, cb) {
|
||||||
|
const uniquePrefix = Date.now();
|
||||||
|
cb(null, uniquePrefix + "-" + file.originalname);
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const upload = multer({ storage: storage });
|
||||||
|
|
||||||
|
module.exports = upload;
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
|
const meetingSchema = new mongoose.Schema(
|
||||||
|
{
|
||||||
|
meetingDate: {
|
||||||
|
type: Date,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
meetingTime: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
mentorName: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
mentiName: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
mentorId: {
|
||||||
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
|
ref: "User",
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
mentiId: {
|
||||||
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
|
ref: "User",
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
type: String,
|
||||||
|
default: "Pending",
|
||||||
|
},
|
||||||
|
amount: {
|
||||||
|
type: Number,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
paymentStatus: {
|
||||||
|
type: "Status",
|
||||||
|
default: "Pending",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const Meeting = mongoose.model("Meeting", meetingSchema);
|
||||||
|
|
||||||
|
module.exports = Meeting;
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
const mongoose = require("mongoose");
|
||||||
|
const bcrypt = require("bcrypt");
|
||||||
|
const jwt = require("jsonwebtoken");
|
||||||
|
const crypto = require("crypto");
|
||||||
|
|
||||||
|
const userSchema = new mongoose.Schema(
|
||||||
|
{
|
||||||
|
name: {
|
||||||
|
type: String,
|
||||||
|
required: [true, "Please Enter your name"],
|
||||||
|
maxLength: [30, "Please Enter the valid name"],
|
||||||
|
minLength: [2, "Name should have more than 5 characters"],
|
||||||
|
},
|
||||||
|
|
||||||
|
skills: [
|
||||||
|
{
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
uniqueRole: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
country: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
email: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
unique: true,
|
||||||
|
lowerCase: true,
|
||||||
|
},
|
||||||
|
password: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
minLength: [6, "Password should have more than 6 characters"],
|
||||||
|
},
|
||||||
|
avatar: {
|
||||||
|
type: String,
|
||||||
|
default: "/images/profile.jpeg",
|
||||||
|
},
|
||||||
|
role: {
|
||||||
|
type: String,
|
||||||
|
default: "user",
|
||||||
|
},
|
||||||
|
mainInterest: [
|
||||||
|
{
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
documents: [
|
||||||
|
{
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
meettingFees: {
|
||||||
|
type: Number,
|
||||||
|
},
|
||||||
|
resetPasswordToken: String,
|
||||||
|
resetPasswordExpiry: Date,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
userSchema.pre("save", async function (next) {
|
||||||
|
if (!this.isModified("password")) return next();
|
||||||
|
|
||||||
|
this.password = await bcrypt.hash(this.password, 10);
|
||||||
|
return next();
|
||||||
|
});
|
||||||
|
|
||||||
|
userSchema.methods.isPasswordCorrect = async function (password) {
|
||||||
|
return await bcrypt.compare(password, this.password);
|
||||||
|
};
|
||||||
|
|
||||||
|
userSchema.methods.generateRefreshToken = async function () {
|
||||||
|
return await jwt.sign(
|
||||||
|
{
|
||||||
|
_id: this._id,
|
||||||
|
email: this.email,
|
||||||
|
},
|
||||||
|
process.env.REFRESH_TOKEN_SECRET
|
||||||
|
// {
|
||||||
|
// expiresIn: process.env.REFRESH_TOKEN_EXPIRY,
|
||||||
|
// }
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
userSchema.methods.getResetPassword = async function () {
|
||||||
|
// Generating token
|
||||||
|
const resetToken = await crypto.randomBytes(20).toString("hex");
|
||||||
|
|
||||||
|
// Hashing and adding reset password token to userschema
|
||||||
|
|
||||||
|
this.resetPasswordToken = crypto
|
||||||
|
.createHash("sha256")
|
||||||
|
.update(resetToken)
|
||||||
|
.digest("hex");
|
||||||
|
|
||||||
|
this.resetPasswordExpiry = Date.now() + 15 * 60 * 1000;
|
||||||
|
|
||||||
|
return resetToken;
|
||||||
|
};
|
||||||
|
|
||||||
|
const User = mongoose.model("User", userSchema);
|
||||||
|
|
||||||
|
module.exports = User;
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
const express = require("express");
|
||||||
|
const {
|
||||||
|
registerUser,
|
||||||
|
loginUser,
|
||||||
|
logoutUser,
|
||||||
|
updateUserDetails,
|
||||||
|
forgetPassword,
|
||||||
|
resetPassword,
|
||||||
|
getUserDetails,
|
||||||
|
updatePassword,
|
||||||
|
updatePersonalDetails,
|
||||||
|
DeleteUser,
|
||||||
|
updateUserRole,
|
||||||
|
intializeUser,
|
||||||
|
updateAvatar,
|
||||||
|
} = require("../Controllers/user.controller.js");
|
||||||
|
|
||||||
|
const { checkAuthenticated } = require("../Middlewares/authentication.js");
|
||||||
|
|
||||||
|
const upload = require("../Middlewares/multer.js");
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.route("/register").post(registerUser);
|
||||||
|
|
||||||
|
router.route("/login").post(loginUser);
|
||||||
|
|
||||||
|
router.route("/password/forgot").post(forgetPassword);
|
||||||
|
|
||||||
|
router.route("/password/reset/:token").put(resetPassword);
|
||||||
|
|
||||||
|
router.route("/logout").get(logoutUser);
|
||||||
|
|
||||||
|
router.route("/update/:id").put(updateUserDetails);
|
||||||
|
|
||||||
|
router.route("/me").get(getUserDetails);
|
||||||
|
|
||||||
|
router.route("/getuser").get(intializeUser);
|
||||||
|
|
||||||
|
router.route("/password/update").put(updatePassword);
|
||||||
|
|
||||||
|
router.route("/me/update").put(updatePersonalDetails);
|
||||||
|
|
||||||
|
router.route("/user/delete/:id").delete(DeleteUser);
|
||||||
|
|
||||||
|
router.route("/user/updateRole/:id").put(updateUserRole);
|
||||||
|
|
||||||
|
router.route("/user/avatar").put(checkAuthenticated(),upload.single("avatar"), updateAvatar);
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
const cloudinary = require("cloudinary").v2;
|
||||||
|
const fs = require("fs");
|
||||||
|
|
||||||
|
const uploadOnCloudinary = async (localFilePath) => {
|
||||||
|
try {
|
||||||
|
if (!localFilePath) return null;
|
||||||
|
|
||||||
|
const responce = await cloudinary.uploader.upload(localFilePath, {
|
||||||
|
resource_type: "auto",
|
||||||
|
});
|
||||||
|
|
||||||
|
// console.log("File is uploaded successfully");
|
||||||
|
fs.unlinkSync(localFilePath, () => {
|
||||||
|
console.log("file removed successfully");
|
||||||
|
});
|
||||||
|
return responce.url;
|
||||||
|
} catch (error) {
|
||||||
|
fs.unlinkSync(localFilePath);
|
||||||
|
console.log("file removed successfully");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
module.exports = { uploadOnCloudinary };
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
const nodemailer = require("nodemailer");
|
||||||
|
|
||||||
|
const sendEmail = async (options) => {
|
||||||
|
const transporter = await nodemailer.createTransport({
|
||||||
|
service: "gmail",
|
||||||
|
host: process.env.HOST,
|
||||||
|
port: process.env.EMAIL_PORT,
|
||||||
|
secure: false,
|
||||||
|
auth: {
|
||||||
|
user: process.env.SMPT_MAIL, // senders email
|
||||||
|
pass: process.env.SMPT_PASSWORD, // app passoword created for your app by using step :: google account> manage your google account >security>enable two step verification > search app password and create password for your app
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const mailOptions = {
|
||||||
|
from: "",
|
||||||
|
to: options.email,
|
||||||
|
subject: options.subject,
|
||||||
|
text: options.message,
|
||||||
|
};
|
||||||
|
|
||||||
|
await transporter.sendMail(mailOptions);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = sendEmail;
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
const express = require("express");
|
||||||
|
const cors = require("cors");
|
||||||
|
const cookieParser = require("cookie-parser");
|
||||||
|
const { Server } = require("socket.io");
|
||||||
|
const { createServer } = require("http");
|
||||||
|
const userRoute = require("./Routes/user.routes.js");
|
||||||
|
const { checkAuthenticated } = require("./Middlewares/authentication.js");
|
||||||
|
const dotenv = require("dotenv");
|
||||||
|
|
||||||
|
dotenv.config({
|
||||||
|
path: "./.env",
|
||||||
|
});
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
const corsOptions = {
|
||||||
|
origin: process.env.FRONTEND_URI,
|
||||||
|
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
|
||||||
|
credentials: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const server = createServer(app);
|
||||||
|
|
||||||
|
const io = new Server(server, {
|
||||||
|
cors: {
|
||||||
|
origin: process.env.FRONTEND_URI,
|
||||||
|
methods: ["GET", "PUT", "POST", "PATCH", "DELETE"],
|
||||||
|
credentials: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use(cors(corsOptions));
|
||||||
|
app.use(express.json({ limit: "16kb" }));
|
||||||
|
app.use(express.urlencoded({ extended: true, limit: "16kb" }));
|
||||||
|
app.use(express.static("public"));
|
||||||
|
app.use(cookieParser());
|
||||||
|
|
||||||
|
app.get("/", (req, res) => {
|
||||||
|
return res.send("Hiddskpkpk...");
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use("/api/v1", userRoute);
|
||||||
|
|
||||||
|
const emailToSocketIdMap = new Map();
|
||||||
|
const socketidToEmailMap = new Map();
|
||||||
|
|
||||||
|
io.on("connection", (socket) => {
|
||||||
|
// console.log(`Socket Connected`, socket.id);
|
||||||
|
socket.on("room:join", (data) => {
|
||||||
|
const { email, room } = data;
|
||||||
|
emailToSocketIdMap.set(email, socket.id);
|
||||||
|
socketidToEmailMap.set(socket.id, email);
|
||||||
|
io.to(room).emit("user:joined", { email, id: socket.id });
|
||||||
|
socket.join(room);
|
||||||
|
io.to(socket.id).emit("room:join", data);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("message", ({ message, room }) => {
|
||||||
|
// console.log(message);
|
||||||
|
// console.log("Message is : ", message, "Room is : ", room);
|
||||||
|
//for sending the message all the users that are connected < --- > io.emit("receive-message", message);
|
||||||
|
//for sending the message for all the user except us <----> socket.broadcast.emit("receive-message", message);
|
||||||
|
socket.join(room);
|
||||||
|
io.to(room).emit("receive-message", message); // For sending the message for perticular room or user
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("user:call", ({ to, offer }) => {
|
||||||
|
io.to(to).emit("incomming:call", { from: socket.id, offer });
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("call:accepted", ({ to, ans }) => {
|
||||||
|
io.to(to).emit("call:accepted", { from: socket.id, ans });
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("peer:nego:needed", ({ to, offer }) => {
|
||||||
|
//console.log("peer:nego:needed", offer);
|
||||||
|
io.to(to).emit("peer:nego:needed", { from: socket.id, offer });
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("peer:nego:done", ({ to, ans }) => {
|
||||||
|
// console.log("peer:nego:done", ans);
|
||||||
|
io.to(to).emit("peer:nego:final", { from: socket.id, ans });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = server;
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
const server = require("./app.js");
|
||||||
|
const dotenv = require("dotenv");
|
||||||
|
const cloudinary = require("cloudinary").v2;
|
||||||
|
const DB_connect = require("./Database/DB_connect.js");
|
||||||
|
|
||||||
|
// dotenv Configuration
|
||||||
|
dotenv.config({
|
||||||
|
path: "./.env",
|
||||||
|
});
|
||||||
|
|
||||||
|
cloudinary.config({
|
||||||
|
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
|
||||||
|
api_key: process.env.CLOUDINARY_API_KEY,
|
||||||
|
api_secret: process.env.CLOUDINARY_API_SECRET,
|
||||||
|
});
|
||||||
|
|
||||||
|
DB_connect();
|
||||||
|
|
||||||
|
// Listening the port
|
||||||
|
server.listen(process.env.PORT, () => {
|
||||||
|
console.log("Server is Running on ", process.env.PORT);
|
||||||
|
console.log("Frontend URI : ", process.env.FRONTEND_URI);
|
||||||
|
});
|
||||||
Generated
+2188
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"name": "backend",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"dev": "nodemon index.js"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"description": "",
|
||||||
|
"dependencies": {
|
||||||
|
"bcrypt": "^5.1.1",
|
||||||
|
"cloudinary": "^2.5.1",
|
||||||
|
"cookie-parser": "^1.4.6",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^16.4.5",
|
||||||
|
"express": "^4.19.2",
|
||||||
|
"jsonwebtoken": "^9.0.2",
|
||||||
|
"mongoose": "^8.6.1",
|
||||||
|
"multer": "^1.4.5-lts.1",
|
||||||
|
"nodemailer": "^6.9.15",
|
||||||
|
"socket.io": "^4.7.5"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"nodemon": "^3.1.4"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"builds": [
|
||||||
|
{
|
||||||
|
"src": "index.js",
|
||||||
|
"use": "@vercel/node"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"src": "/api/v1/(.*)",
|
||||||
|
"dest": "index.js"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"src": "/(.*)",
|
||||||
|
"dest": "index.js"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user