Create and Test User and Farm Routes

This commit is contained in:
2025-02-22 17:03:24 +05:30
parent 1fdb739950
commit 330589bdf1
14 changed files with 617 additions and 27 deletions
+24 -21
View File
@@ -1,32 +1,35 @@
const jwt = require("jsonwebtoken");
const User = require("../Models/user.model.js");
const dotenv = require("dotenv");
dotenv.config({
path: "./.env",
});
function checkAuthenticated() {
return async (req, res, next) => {
const tokenValue = req.cookies[process.env.TOKEN_NAME];
async function checkAuthenticated(req, res, next) {
const tokenValue = req.cookies[process.env.TOKEN_NAME];
// console.log("I am called",tokenValue);
if (!tokenValue) {
console.log("I am called", 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 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();
}
req.user = payload;
return next();
};
} catch (error) {
return next();
}
}
function authorizeRoles(...roles) {