From ebc42abefc859700b2e156c19c673c5e8f5803f3 Mon Sep 17 00:00:00 2001 From: Hemu21 Date: Tue, 21 May 2024 14:54:33 +0530 Subject: [PATCH 1/7] cook --- backend/.env | 10 +++++----- backend/app.js | 4 ++++ .../app/routes/Q&A/question/downvoteQuestion.js | 2 ++ backend/app/routes/Q&A/question/index.js | 5 +++-- .../app/routes/Q&A/question/upvoteQuestion.js | 4 +++- backend/helpers/middlewares/cookie.js | 16 ++++++++++++++++ backend/package.json | 1 + frontend/.env | 2 +- 8 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 backend/helpers/middlewares/cookie.js diff --git a/backend/.env b/backend/.env index b2ab43ac..180f764d 100644 --- a/backend/.env +++ b/backend/.env @@ -1,10 +1,10 @@ -MONGO_DB_URL= -JWT_SECRET_KEY= +MONGO_DB_URL=mongodb+srv://hemanth_gssoc:bSEGOLYZV2mzYCAR@cluster0.dbhtp.mongodb.net +JWT_SECRET_KEY=eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTcxNTYxODA2NCwiaWF0IjoxNzE1NjE4MDY0fQ.injvI2GcyLY7JlnYuaBbaeOszItsabjV_C1rGuVh5IM JWT_EXPIRES_IN=6h BASE_URL=https://community-website-backend.herokuapp.com -EMAIL_USER=EMAIL_HERE -EMAIL_PASS=PASSWORD_HERE +EMAIL_USER=gujjalahemanthkumar789@gmail.com +EMAIL_PASS=20052018@Hk EMAIL_HOST=smtp.gmail.com -CLUSTER=no +CLUSTER=yes JWT_RESET_PASSWORD_EXPIRES_IN=1h LOCAL_DEV_ENV=http://localhost:3500/ diff --git a/backend/app.js b/backend/app.js index 28fb8e6f..2c6a34c5 100644 --- a/backend/app.js +++ b/backend/app.js @@ -4,6 +4,7 @@ const helmet = require('helmet'); const responseTime = require('response-time'); const routes = require('./app/routes'); const { errorHandler } = require('./helpers/error'); +const cookieParser = require('cookie-parser'); require('colors'); require('./helpers/dbConnection'); @@ -24,6 +25,9 @@ app.use(express.urlencoded({ limit: '50mb', extended: true })); // Response time app.use(responseTime({ suffix: false })); +// cookie +app.use(cookieParser()); + // Use routes app.use('/', routes); diff --git a/backend/app/routes/Q&A/question/downvoteQuestion.js b/backend/app/routes/Q&A/question/downvoteQuestion.js index 4fbb3731..d29b7869 100644 --- a/backend/app/routes/Q&A/question/downvoteQuestion.js +++ b/backend/app/routes/Q&A/question/downvoteQuestion.js @@ -2,6 +2,7 @@ const to = require('await-to-js').default; const question = require('../../../models/question'); const { ErrorHandler } = require('../../../../helpers/error'); const constants = require('../../../../constants'); +const { getVoteCookieName } = require('../../../../helpers/middlewares/cookie'); module.exports = async (req, res, next) => { const { questionId } = req.body; @@ -35,6 +36,7 @@ module.exports = async (req, res, next) => { return next(error); } + res.cookie(getVoteCookieName('question', questionId), true, { maxAge: 20 * 365 * 24 * 60 * 60 * 1000 }); res.status(200).send({ message: 'Question has been down voted', }); diff --git a/backend/app/routes/Q&A/question/index.js b/backend/app/routes/Q&A/question/index.js index 2f1c35dd..19df08db 100644 --- a/backend/app/routes/Q&A/question/index.js +++ b/backend/app/routes/Q&A/question/index.js @@ -9,6 +9,7 @@ const downvoteQuestion = require('./downvoteQuestion'); const updateQuestionStatus = require('./updateQuestionStatus'); const { authMiddleware } = require('../../../../helpers/middlewares/auth'); const deleteQuestion = require('./deleteQuestion'); +const { checkVoteCookie } = require('../../../../helpers/middlewares/cookie'); router.post('/', validation(QuestionValidationSchema), postQuestion); @@ -19,10 +20,10 @@ router.get('/getallquestions', getAllQuestion); router.get('/getQuestionById/:questionId', getQuestionById); // This route will increase upvote by one. -router.patch('/upvote', upvoteQuestion); +router.patch('/upvote',checkVoteCookie, upvoteQuestion); // This route will decrease upvote by one. -router.patch('/downvote', downvoteQuestion); +router.patch('/downvote',checkVoteCookie, downvoteQuestion); // route for updating the question status router.patch('/updateStatus', validation(updateQuestionStatusSchema), updateQuestionStatus); diff --git a/backend/app/routes/Q&A/question/upvoteQuestion.js b/backend/app/routes/Q&A/question/upvoteQuestion.js index 63b28fd6..77a1b2a2 100644 --- a/backend/app/routes/Q&A/question/upvoteQuestion.js +++ b/backend/app/routes/Q&A/question/upvoteQuestion.js @@ -2,6 +2,7 @@ const to = require('await-to-js').default; const question = require('../../../models/question'); const { ErrorHandler } = require('../../../../helpers/error'); const constants = require('../../../../constants'); +const { getVoteCookieName } = require('../../../../helpers/middlewares/cookie'); module.exports = async (req, res, next) => { const { questionId } = req.body; @@ -12,10 +13,11 @@ module.exports = async (req, res, next) => { message: 'Database Error', errStack: err, }); - return next(error); } + res.cookie(getVoteCookieName('question', questionId), true, { maxAge: 20 * 365 * 24 * 60 * 60 * 1000 }); + res.status(200).send({ message: 'Question has been upvoted', }); diff --git a/backend/helpers/middlewares/cookie.js b/backend/helpers/middlewares/cookie.js new file mode 100644 index 00000000..bf7bd680 --- /dev/null +++ b/backend/helpers/middlewares/cookie.js @@ -0,0 +1,16 @@ +const getVoteCookieName = (type, id) => `${type}_voted_${id}`; + +const checkVoteCookie = (req, res, next) => { + const { questionId, answerId } = req.body; + const voteType = questionId ? 'question' : 'answer'; + const voteId = questionId || answerId; + const voteCookieName = getVoteCookieName(voteType, voteId); + + if (req.cookies[voteCookieName]) { + return res.status(400).json({ error: 'You have already voted' }); + } + + next(); +}; + +module.exports = { checkVoteCookie, getVoteCookieName } \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index e470d723..b9cb2775 100644 --- a/backend/package.json +++ b/backend/package.json @@ -28,6 +28,7 @@ "chai-http": "^4.3.0", "colors": "^1.4.0", "config": "^3.3.3", + "cookie-parser": "^1.4.6", "cors": "^2.8.5", "dotenv": "^8.2.0", "ejs": "^3.1.6", diff --git a/frontend/.env b/frontend/.env index c0da241b..c4ce10e3 100644 --- a/frontend/.env +++ b/frontend/.env @@ -1 +1 @@ -REACT_APP_API="https://community-website-backend.onrender.com" \ No newline at end of file +REACT_APP_API="http://localhost:3500" \ No newline at end of file From 97ce747b10be8885aa084da416d669eb545e0777 Mon Sep 17 00:00:00 2001 From: Hemanth kumar Date: Tue, 21 May 2024 14:59:27 +0530 Subject: [PATCH 2/7] Update .env --- backend/.env | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/.env b/backend/.env index 180f764d..b2ab43ac 100644 --- a/backend/.env +++ b/backend/.env @@ -1,10 +1,10 @@ -MONGO_DB_URL=mongodb+srv://hemanth_gssoc:bSEGOLYZV2mzYCAR@cluster0.dbhtp.mongodb.net -JWT_SECRET_KEY=eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTcxNTYxODA2NCwiaWF0IjoxNzE1NjE4MDY0fQ.injvI2GcyLY7JlnYuaBbaeOszItsabjV_C1rGuVh5IM +MONGO_DB_URL= +JWT_SECRET_KEY= JWT_EXPIRES_IN=6h BASE_URL=https://community-website-backend.herokuapp.com -EMAIL_USER=gujjalahemanthkumar789@gmail.com -EMAIL_PASS=20052018@Hk +EMAIL_USER=EMAIL_HERE +EMAIL_PASS=PASSWORD_HERE EMAIL_HOST=smtp.gmail.com -CLUSTER=yes +CLUSTER=no JWT_RESET_PASSWORD_EXPIRES_IN=1h LOCAL_DEV_ENV=http://localhost:3500/ From b5add11a44ecc958bdb80d604e067fbc44280790 Mon Sep 17 00:00:00 2001 From: Hemanth kumar Date: Tue, 21 May 2024 15:00:11 +0530 Subject: [PATCH 3/7] Update .env --- frontend/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/.env b/frontend/.env index c4ce10e3..ad1ac9f1 100644 --- a/frontend/.env +++ b/frontend/.env @@ -1 +1 @@ -REACT_APP_API="http://localhost:3500" \ No newline at end of file +REACT_APP_API="https://community-website-backend.onrender.com" From 2dd46680c0ed040fd720079d75533662e76beb41 Mon Sep 17 00:00:00 2001 From: Hemanth kumar Date: Tue, 21 May 2024 15:00:41 +0530 Subject: [PATCH 4/7] Update .env From 37d1097b22d53c5753c4f3aab4978e4b28127cda Mon Sep 17 00:00:00 2001 From: Hemanth kumar Date: Tue, 21 May 2024 15:01:18 +0530 Subject: [PATCH 5/7] Update .env --- frontend/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/.env b/frontend/.env index ad1ac9f1..a8b590ae 100644 --- a/frontend/.env +++ b/frontend/.env @@ -1 +1 @@ -REACT_APP_API="https://community-website-backend.onrender.com" +REACT_APP_API="https://community-website-backend.onrender.com" From 6f24987f09ccbdb59b0c3e75a45cc40aa5b5373f Mon Sep 17 00:00:00 2001 From: Hemanth kumar Date: Tue, 21 May 2024 15:02:41 +0530 Subject: [PATCH 6/7] Update .env --- frontend/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/.env b/frontend/.env index a8b590ae..ad1ac9f1 100644 --- a/frontend/.env +++ b/frontend/.env @@ -1 +1 @@ -REACT_APP_API="https://community-website-backend.onrender.com" +REACT_APP_API="https://community-website-backend.onrender.com" From f05707e6e93cc25d7ab2d3eaf0540519e573285f Mon Sep 17 00:00:00 2001 From: Hemanth kumar Date: Wed, 22 May 2024 15:20:50 +0530 Subject: [PATCH 7/7] Update .env