在我正在开发的产品(将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;
});
});
答案 0 :(得分:1)
一种方法是通过引入单选按钮来选择角色来扩展逻辑,然后在提交时,它将在表单提交时调用特定的处理程序(addAdmin / addSuperAdmin / addContextAdmin)。
如果选择了“用户管理”广播btn,请调用addAdmin
,这反过来会调用addAdminRole
云功能。
类似地,您可以引入其他两个云函数(在index.js中)-> addSuperAdminRole
和addContextAdminRole
。
然后根据选中的单选按钮调用相关处理程序。