Angularjs 在更新/删除文档之前,如何检查Passport JS中req.user的userID和MongdoDB userID是否匹配?

Angularjs 在更新/删除文档之前,如何检查Passport JS中req.user的userID和MongdoDB userID是否匹配?,angularjs,node.js,mongodb,express,Angularjs,Node.js,Mongodb,Express,我正在为使用Passport JS+进行身份验证的用户制作一个带有删除和编辑功能的投票应用程序,具体投票属于他/她 我有以下护照设置和节点/Express: passport.use(new FacebookStrategy({ clientID: FACEBOOK_APP_ID, clientSecret: FACEBOOK_APP_SECRET, callbackURL: CALLBACK_URL }, function(accessToken, refreshToken

我正在为使用
Passport JS
+进行身份验证的用户制作一个带有
删除和编辑功能的投票应用程序,具体投票属于他/她

我有以下
护照设置
节点/Express

 passport.use(new FacebookStrategy({ 

  clientID: FACEBOOK_APP_ID,
  clientSecret: FACEBOOK_APP_SECRET,
  callbackURL: CALLBACK_URL

}, function(accessToken, refreshToken, profile, done){  
//callback function after login  

  process.nextTick(function(){        
    done(null, profile);        
  });      
}));


passport.serializeUser(function(user,done){

req.session.passport.user = {};  

  done(null, user);

});

passport.deserializeUser(function(id, done){

  done(null, id);

});


app.get('/auth/facebook', passport.authenticate('facebook'));


app.get('/auth/facebook/callback', passport.authenticate('facebook', { 
    successRedirect: '/',
    failureRedirect: '/error'

}));

app.get('/success', function(req, res, next) { 

  res.send('Successfully logged in. You can close this window');      
});


app.get('/error', function(req, res, next) {    
  res.send("Error logging in.");      
});
因此,现在每个请求都有一个附加了id和displayName的用户(req.user)

我使用以下中间件功能来保护路由服务器端:

var auth = function(req,res,next){

    if(!req.isAuthenticated()){

      console.log("You need to login!");

      res.sendStatus(401);                    
    } 
    else {

      console.log("User is logged in, success!");          
      next();          
    }              
};
轮询以以下格式保存在MongoDB中:

{
    "_id": {
        "$oid": "57c69ec65ed24f166b4e98cf"
    },
    "title": "Poll Test",
    "options": [
        {
            "option": "Option1",
            "votes": 0
        },
        {
            "option": "Option2",
            "votes": 0
        },
        {
            "option": "Option3",
            "votes": 0
        },
        {
            "option": "Option4",
            "votes": 0
        }
    ],
    "userID": "1798846357",
    "displayName": "John Doe",
    "date": "Wed Aug 31 2016 09:09:26 GMT+0000 (UTC)"
}
我想保护下面的路由不受REST API的影响,例如前端中的
delete poll
函数使用了REST API

问题是它检查用户是否经过身份验证,但不检查轮询是否属于该用户。换句话说:如果请求中的
userID
与数据库中的
userID
匹配,则不会

我试图在
db.collection
函数中放入一条if语句,以检查
req.user.id和doc.data.id是否匹配
,但它不起作用

我想那是因为updateOne已经完成了

如何使用userID检索轮询,然后使用req.user.id检查轮询,如果匹配,则继续更新

    app.delete("/polls/:id", auth, function(req, res) {         
    db.collection(POLLS_COLLECTION).deleteOne({
      _id: new ObjectID(req.params.id)
    }, function(err, doc) {

      if (err) {

        handleError(res, err.message, "Failed to delete contact");

      } else {

        res.status(204).end();
      }    
    });        
  });        
});
这可能会帮助您:

问题的答案

如何使用userID检索轮询,然后使用req.user.id检查,如果匹配,则继续更新?

如果存在与值
req.user.id
相同的
userID
文档,则上述查询将更新文档。否则将不会有任何更新


希望这会有所帮助。

使用
deleteOne
updateOne
filter
参数进行检查:

db.collection(POLLS_COLLECTION).deleteOne({
      _id: new ObjectID(req.params.id),
      userID: req.user._id,
    }, function(err, doc) {
    …
    });
  });
您还可以首先获取投票对象并使用
。然后在该查询上使用
,仅当条件匹配时才删除,但在这种情况下,您会遇到并发问题,而不会出现写锁。(当您检查投票是否属于该用户时,有人可能会更改投票对象的用户ID–您会怎么做?)

db.collection(POLLS_COLLECTION).deleteOne({
      _id: new ObjectID(req.params.id),
      userID: req.user._id,
    }, function(err, doc) {
    …
    });
  });