簡體   English   中英

如何使用useMasterKey與Node.js進行解析?

[英]How to use the useMasterKey with Node.js in parse?

大家好,我在parse.com制作了我的應用程序,但是我想使用主密鑰,因為我有用戶,具有administrador角色。

首先,我嘗試使用JS SDk,但他們告訴我,我必須使用Cloud Code。

好吧,我嘗試使用Express和Cloud Code,但是當我嘗試使用Parse.Cloud.useMasterkey()時,控制台會說:

部署失敗,並顯示以下錯誤:未捕獲的Parse.initialize()傳遞了一個主密鑰,該密鑰僅可從Node.js內部使用。

大豆,我的問題是如何在解析中使用Node.js? 還是沒有Node的MasterKey無法使用?

干杯:)

好吧,這是我的代碼,管理員用戶想要添加新員工,但只有他可以這樣做,因此在數據瀏覽器中,該用戶是主密鑰

exports.create = function(req, res){

   Parse.Cloud.useMasterKey();

   var nombre = req.body.nombre;
   var apellido = req.body.apellido;
   var email = req.body.email;
   var telefono =req.body.telefono;
   var celular = req.body.celular;
   var username = req.body.username;
   var password = req.body.pwd;

   //set data in empleado and users
   empleado.set("nombre", nombre);
   empleado.set("apellido", apellido);
   empleado.set("email", email);
   empleado.set("telefono", parseInt(telefono));
   empleado.set("celular", parseInt(celular));
   user.set("username", username);
   user.set("password", password);
   user.set("email", email);

  /* //crear a pointer in Users tables with information of employee 
   user.set("informacionAdicional", empleado);
   user.save();*/

   empleado.save(null,{
    success: function(empleado){
        console.log('Se han guardado los datos del empleado');},
        error: function (error){
             alert("Error"+ error.code + "" + error.message);}
     });


};

您不應該使用Web上的主密鑰,那是不安全的。 您需要做的是為您的數據表分配正確的權限。

首先,閱讀數據安全性: https : //parse.com/docs/data#security

您應該做的是向要創建的類添加正確的寫權限。 在這種情況下,這可能是Employee類。 確保只有管理員用戶可以寫入此表。 也許您可以添加一個Administrators角色並為此角色添加寫權限。

使用javascript SDK,用戶首先需要進行身份驗證。 如果經過身份驗證的用戶是管理員角色的一部分,則該用戶將能夠添加用戶。

您不需要使用主密鑰,並且永遠不要在客戶端上使用主密鑰。 那將是不安全的。 如果您在客戶端公開主密鑰,則任何人都可以讀取該密鑰並獲得對數據庫的完全訪問權限。

這里有一些關於完整實現的說明。

首先在html中確保您具有以下內容:

<script src="//www.parsecdn.com/js/parse-1.3.5.min.js"></script>

然后,您需要初始化解析並定義一個函數來創建用戶,例如:

Parse.initialize("your application id goes here", "your javascript key goes here");

var createEmployee = function(employeeData){

    var Employee = Parse.Object.extend("Employee");
    var employee = new Employee();

    employee.set("name", employeeData.name);

    //Set more properties of the user/employee


    employee.save(null, {
      success: function(gameScore) {
        // Execute any logic that should take place after the object is saved.
        alert('New employee created with objectId: ' + employee.id);
      },
      error: function(gameScore, error) {
        // Execute any logic that should take place if the save fails.
        // error is a Parse.Error with an error code and message.
        alert('Failed to create new object, with error code: ' + error.message);
      }
    });

};

然后,當您需要創建員工時,請調用以下函數:

createEmployee({ name: "Some name" });

暫無
暫無

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

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