繁体   English   中英

createUserWithEmailAndPassword firebase auth in ReactJS创建用户时如何区分管理员和客户角色

[英]How to distinguish admin and customer role when create user with createUserWithEmailAndPassword firebase auth in ReactJS

我有两个 reactjs 网站,一个用于客户端用户,一个用于管理员。 我有 graphql api 用于数据访问。 目前admin使用的是JWT,我们将改为firebase认证。

我已经使用 firebase package 实现了登录和注册。

这里是创建用户 function。所以它工作并且可以成功登录。 但我的问题是如何区分管理员用户和客户用户。 我看到 setCustomUserClaims 仅适用于 firebase-admin。 请问firebase中的角色怎么设置,正确的设置方法是什么?

function register(name, email, password) {
return createUserWithEmailAndPassword(auth, email, password).then((res) => {
  const auth = getAuth();
  
  updateProfile(auth.currentUser, {
    displayName: name
  }).then(() => {
    // Profile updated!
    // ...
  }).catch((error) => {
    // An error occurred
    // ...
    console.log(error.message);
  });
});

}

自定义声明绝对是在 Firebase Auth 中添加角色的最简单方法,但您需要在Cloud function或任何安全服务器环境中使用Admin SDK

我看到setCustomUserClaims仅适用于 firebase firebase-admin

setCustomUserClaims()用于设置自定义声明,但您始终可以在客户端 SDK 中使用getIdTokenResult() function 读取它们。

const { claims } = await getIdTokenResult(auth.currentUser)

因此,如果您的 GraphQL API 在您自己的服务器上运行,您可以在那里安装 Firebase Admin 并将自定义声明添加到 Firebase 身份验证用户。

或者,您可以将用户角色存储在Firestore等数据库或您自己的数据库中,然后从那里读取用户角色。 数据库中存储角色的一个问题是,如果需要,您无法从其他服务(如 Firebase 存储)的安全规则访问它们,但自定义声明可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM