Answer the question
In order to leave comments, you need to log in
How to prevent unauthorized users from CREATE and DELETE operations in mongodb?
I am writing an application in Node.js. I want to make sure that only users authorized in the system can add data to some collections (authorization using the passport-local strategy).
For example, there is a collection of user questions "questions". A question can only be added by a user logged in to the system.
Now I do the following: Before each request I write if(!ctx.isAuthenticated()) return;
It looks like this:
//страница обрабатывающая запрос
const Questions = require('../../models/questions.js');
exports.post = async (ctx, next) => {
if(!ctx.isAuthenticated()) return;
const insertedQuestion = new Questions({
text:ctx.request.body.text
})
ctx.body = await insertedQuestion.save()
}
//questionsSchema
const mongoose = require('../libs/mongoose');
const questionsSchema = new mongoose.Schema({
text: String,
})
module.exports = mongoose.model('Questions', questionsSchema);
Answer the question
In order to leave comments, you need to log in
There is too much in one module, passport js will authorize hang up sessions, acl will do the rights, work out the logic in it. The passport has an isAutentificate method, add it to the routes as middleware, make routes in general, separate the logic. What kind of nonsense and not code?)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question