Firebase多种管理员类型

问题描述

在我正在开发的产品(将ReactJS和Firebase Auth与Firestore结合使用)中,我需要设置以下帐户类型:

超级管理员
内容管理员
用户管理员

当前,我有一个表单,一旦输入了用户电子邮件,它将为具有该电子邮件地址的用户提供一个管理员”令牌。

使用3种不同的管理员类型,做到这一点的最佳方法是什么?

我已经包含设置管理员令牌的代码

AddAdmin.js

import React,{ Component } from 'react'
import './AddAdmin.scss'
const firebase = require("firebase");

class AddAdmin extends Component {
    state = {
      superAdminemail: ''
    }
  
    updateAdminemail = (e) => {
      this.setState({
        adminemail: e.target.value
      })
    }
  
    addAdmin = (e) => {
      e.preventDefault();
      const addAdminRole = firebase.functions().httpsCallable('addAdminRole');
      addAdminRole({email: this.state.adminemail})
        .then(result => {
          console.log(result);
          })
    }
  
    render() {
      return (
          <div className = "AddAdminForm">
              <form className = "admin-actions" onSubmit={this.addAdmin}>
                  <input type = "email" placeholder = "User email" id = "admin-email" value={this.state.adminemail} onChange={this.updateAdminemail} required/>
                  <button type="submit"> Make Admin </button>
              </form>
          </div>
        
      )
    }
  }
export default AddAdmin

Index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.addAdminRole = functions.https.onCall((data,context) => {
  // get user and add admin custom claim
  return admin.auth().getUserByEmail(data.email).then(user => {
    return admin.auth().setCustomUserClaims(user.uid,{
      admin: true
    })
  }).then(() => {
    return {
      message: `Success! ${data.email} has been made an admin.`
    }
  }).catch(err => {
    return err;
  });
});

解决方法

一种方法是通过引入单选按钮来选择角色来扩展逻辑,然后在提交时,它将在表单提交时调用特定的处理程序(addAdmin / addSuperAdmin / addContextAdmin)。

如果选择了“用户管理”广播btn,请调用addAdmin,这反过来会调用addAdminRole云功能。

类似地,您可以引入其他两个云函数(在index.js中)-> addSuperAdminRoleaddContextAdminRole

然后根据选中的单选按钮调用相关处理程序。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...