首页 > 解决方案 > 如何仅将具有查看者权限的新用户添加到 console.firebase 项目中的特定应用程序

问题描述

我只能在 console.firebase.google.com 上为项目添加一个新用户,以访问它拥有的所有应用程序。我想限制用户从项目中授予特定应用程序的权限,而不是为应用程序创建新项目。这可行吗?

标签: firebaseuser-controlssettingsfirebase-console

解决方案


此行为不是内置的,因为应用程序是为项目的版本控制而设计的。但是,您可以通过对用户使用自定义声明来实现这一点。这确实需要安装计费帐户和 Cloud Functions。但是在注册时,您可以在完成时调用云函数,该函数将调用云函数以向用户添加自定义声明。

例如

exports.setApp= functions.https.onCall((data, context) => {
if(!data.app && ! context.auth) return;
if(!["appName1","appName2"].includes(data.app)) return;
return admin
  .auth()
  .setCustomUserClaims(context.auth.uid, { app: data.app})
  .then(() => {
    // The new custom claims will propagate to the user's ID token the
    // next time a new one is issued.
  });
  // ...
});

完成此操作后,您必须在完成时刷新用户令牌以确保 id 令牌具有更新的声明。

您可以在此处阅读有关此内容的更多信息:https ://firebase.google.com/docs/auth/admin/custom-claims


推荐阅读