【发布时间】:2020-04-09 02:09:44
【问题描述】:
我正在创建一个带有身份验证和使用 firebase 云功能的反应应用程序。我还使用 customClaims 设置用户角色,例如管理员、版主、编辑和用户。我还有一个 Firestore 文档,我在其中存储公司信息等用户信息,并且我有一个功能供管理员(我自己)创建其他用户。
但是,如果我想允许版主创建用户但只允许他们创建连接到他们自己公司的用户,我该怎么做。他们应该能够为自己的公司设置编辑甚至版主,但不能为另一家公司创建用户。
我知道我能够在客户端上直接根据加载给新用户的自己的用户配置文件传递用户信息。但这可能是一个安全问题,对吧?据我了解,最好的方法是在firebase云功能中自动添加信息?但是我需要同时访问我自己的凭据和新的用户凭据。这甚至可能吗?
exports.testfunc = (req, res) => {
const customClaims = {
moderator: true,
subscription: true
};
const newUser = {
moderator: req.body.role,
subscription: true,
company: req.body.company //Want to set this one to the creators company. For example if my company is named Awsome inc then the new user gets the same.
}
return admin
.auth()
.getUserByEmail(req.body.email)
.then(userRecord => {
db.doc(`/usr/${userRecord.uid}`).update(customClaims); //Updates the user doc
return admin.auth().setCustomUserClaims(userRecord.uid, customClaims); //Setting the custom claims
})
.then(() => {
console.log(`Successfully updated `);
return res.status(201).json("Success");
})
.catch(err => {
return res.status(500).json({ error: err.code });
});
};
有没有更好的方法来实现这一点?对这个问题有什么想法吗?
【问题讨论】:
标签: reactjs firebase react-native google-cloud-firestore google-cloud-functions