簡體   English   中英

通過REST端點使用JSON對象更新Mongoose模式

[英]Updating Mongoose schema with JSON object via REST endpoint

我有一個正在與JSON對象(用戶)一起提交的REST端點,我只是將相應的mongo記錄設置為該JSON對象。 這為我省去了在服務方法和端點中更新架構更改的麻煩,而只剩下Mongoose模型來更新。

如果有的話,哪種方法更安全?

示例用戶JSON

{
  'fname': 'Bill',
  'lname': 'Williams',
  'email': 'bill@billwilliams.com',
  'settings': {
    'strokeColor': '#FF0000'
  }
}

通過我的Angular服務

Update: function(my_user) {
  return $http.put('http://api.domain.com/v1/api/users/' + _user.id, {
    user: my_user,
    token: window.localStorage['token']
  });
}

我在Node中的REST端點

api.route('/users/:user_id')
  .put(function(req, res) {

    User.findById(req.params.user_id, function(err, user) {
      userData = req.body.user;

      if (user) {
        //-- This is potential trouble area?
        User.update({'_id': user._id}, {$set: userData});

      user.save(function(err) {
          res.json({
            success: true,
            message: 'User updated'
          });
  }); //-- end findById()
}); //-- end /users/:user_id put() route

看看Jsonwebtoken

它基本上是這樣的:

  1. 創建一個REST端點,該端點使用戶可以獲取具有特定負載(例如ID)的令牌
  2. 使用Jsonwebtoken中間件保護api的相關部分(如果使用express作為網絡服務器)
  3. 用戶將令牌添加到每個請求標頭(通過使用$ httpInterceptor)
  4. 在請求到達您的API 之前 ,在服務器端檢查令牌

令牌可能會在一定時間后過期(在用戶需要首先注冊時很有用),從而增加了安全性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM