简体   繁体   English

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

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

I have two reactjs websites, one for client user and one for admin.我有两个 reactjs 网站,一个用于客户端用户,一个用于管理员。 I have graphql api for data access.我有 graphql api 用于数据访问。 Currently, admin is using JWT and we will change to firebase authentication.目前admin使用的是JWT,我们将改为firebase认证。

I already implement both login and registration using firebase package.我已经使用 firebase package 实现了登录和注册。

here is the create user function. So it worked and can login successfully.这里是创建用户 function。所以它工作并且可以成功登录。 But my question is that how can I distinguish admin user and customer user.但我的问题是如何区分管理员用户和客户用户。 I saw that setCustomUserClaims is only for firebase-admin.我看到 setCustomUserClaims 仅适用于 firebase-admin。 May I know how to set the role in firebase and what is the correct way to set?请问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);
  });
});

} }

Custom claims are definitely the easiest way to add roles in Firebase Auth but you'll need to use Admin SDK in a Cloud function or any secure server environment.自定义声明绝对是在 Firebase Auth 中添加角色的最简单方法,但您需要在Cloud function或任何安全服务器环境中使用Admin SDK

I saw that setCustomUserClaims is only for firebase-admin我看到setCustomUserClaims仅适用于 firebase firebase-admin

setCustomUserClaims() is used to set the custom claims but you can always read them using getIdTokenResult() function in client SDK. setCustomUserClaims()用于设置自定义声明,但您始终可以在客户端 SDK 中使用getIdTokenResult() function 读取它们。

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

So if your GraphQL API runs on your own server you can install Firebase Admin there and add custom claims to Firebase auth users.因此,如果您的 GraphQL API 在您自己的服务器上运行,您可以在那里安装 Firebase Admin 并将自定义声明添加到 Firebase 身份验证用户。

Alternatively, you can store user role in a database like Firestore or your own database and read user's role from there.或者,您可以将用户角色存储在Firestore等数据库或您自己的数据库中,然后从那里读取用户角色。 One catch of store roles in a database is that you cannot access them from security rules of other services like Firebase storage if required but custom claims can be.数据库中存储角色的一个问题是,如果需要,您无法从其他服务(如 Firebase 存储)的安全规则访问它们,但自定义声明可以。

暂无
暂无

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

相关问题 Firebase 错误 auth/admin-restricted-operation 在 createUserWithEmailAndPassword 之后 - Firebase Error auth/admin-restricted-operation after createUserWithEmailAndPassword Firebase 在 React 应用程序中验证 createUserWithEmailAndPassword - Firebase Auth createUserWithEmailAndPassword in React app firebase 和 reactjs 中的管理面板和用户登录 - Admin panel and user login in firebase and reactjs email未验证时如何注销用户,Firebase Auth - How to sign out the user when the email is not verified, Firebase Auth 我正在尝试将用户信息添加到 firebase createuserwithemailandpassword - i am trying to add user information to firebase createuserwithemailandpassword firebase createUserWithEmailAndPassword 在 android 中不起作用 - firebase createUserWithEmailAndPassword is not working in android 如何在 firebase 的“createUserWithEmailAndPassword”身份验证方法中包含用户名 - How to include username in firebase's 'createUserWithEmailAndPassword' authentication method Flutter:MissingPluginException(未在通道插件上找到方法 createUserWithEmailAndPassword 的实现。flutter.io/firebase_auth) - Flutter: MissingPluginException(No implementation found for method createUserWithEmailAndPassword on channel plugins.flutter.io/firebase_auth) Admin SDK 的 Firebase 身份验证限制? - Firebase Auth Limit for Admin SDK? 如何在 GCP/Firebase 中创建 Central Auth 项目? - How To Create A Central Auth Project In GCP/Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM