2016-08-31 4 views
1

私はPassport JSを使って認証されたユーザーのためにdelete and edit機能を持つ投票アプリを作成しています+特定の投票は彼/彼女に属します。Passport JSとMongdoDBユーザーIDのreq.userからuserIDが文書の更新/削除前に一致するかどうかをチェックする方法は?

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.");  
}); 

ので、すべての要求は、今、私は以下の使用DisplayNameおよびId

でそれ(req.user)に添付のユーザーを持っています

私はPassport setupNode/Expressと、次の持っていますルートサーバー側を確保するためのミドルウェア機能:

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();   
    }    
}; 

世論調査はfol MongoDBの中lowing形式:

{ 
    "_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)" 
} 

私はフロントエンドにdelete poll機能例えばによって使用されている私のREST APIから次のルートを保護したいです。

問題は、ユーザーが認証されているかどうかを確認することですが、ユーザーにポーリングが含まれているかどうかはチェックされません。つまり、要求のuserIDとデータベースのuserIDが一致していない場合

db.collectionの中にifステートメントを入れて、req.user.id and doc.data.id matchが機能していないかどうかを確認しようとしましたが、機能しません。

updateOneは既に行われていると思います。

ユーザーIDで投票を取得し、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(); 
     }  
    });   
    });   
}); 

答えて

2

これはあなたを助けるかもしれない:任意の文書が存在した場合、質問

How can I retrieve the poll with the userID and then check that with the req.user.id and if match, continue with the update?

db.collectionName.updateOne({userID : req.user.id}, {updateQuery}); 

回答は、上記のクエリは、文書を更新しますuserIDと同じ値req.user.id。それ以外の更新はありません。

希望すると、これが役立ちます。 deleteOneまたはupdateOnefilterパラメータと

1

チェック:

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

また、最初のあなたの投票のオブジェクトをフェッチし、条件が一致した場合にのみ削除するようにそのクエリに.thenを使用しますが、その場合には、自分自身を開くことができます書き込みロックのない並行性の問題まで。 (誰かが投票オブジェクトのuserIDを変更する可能性があります。その投票がそのユーザーに属しているかどうかを確認しています - あなたは何をしますか?)

関連する問題