无需登录用户Parse Server JS SDK即可调用云代码

问题描述

是否可以调用没有对象的云函数来返回对象? iOS和Android SDK支持匿名用户,但我专门要求使用JavaScript。

我想允许它,以便访问我的Web应用程序的任何人都可以读取对象而无需登录。我正在使用Back4App。

解决方法

是的。无论用户是否登录,都可以调用云代码功能。在云功能内部,您可以检查user对象的request属性,以检查用户是否登录。如果您的用户未登录并且要查询需要用户权限的类,则可以使用useMasterKey选项。

Parse.Cloud.define('myFunction',async req => {
  const { user,isMaster } = req;

  if (isMater) {
    // the cloud code function was called using master key
  } else if (user) {
    // the cloud code function was called by an authenticated user
  } else {
    // the cloud code function was called without passing master key nor session token - not authenticated user
  }

  const obj = new Parse.Object('MyClass');
  await obj.save(null,{ useMasterKey: true }); // No matter if the user is authenticated or not,it bypasses all required permissions - you need to know what you are doing since anyone can fire this function

  const query = new Parse.Query('MyClass');
  return query.find({ useMasterKey: true }) // No matter if the user is authenticated or not,it bypasses all required permissions - you need to know what you are doing since anyone can fire this function
});