简体   繁体   English

向注册用户 Firebase 添加额外信息

[英]Adding extra information to registration user Firebase

I would like to make an application in React Native that allows to work in two modes, parent and child.我想在 React Native 中制作一个应用程序,允许在两种模式下工作,父母和孩子。 The initial stage is registration with Firebase, then after adding additional information about the role (parent / child), registering both the child and parent, and after logging in both of them, the child will share location and parent will receive it.初始阶段是在 Firebase 注册,然后在添加有关角色(父/子)的附加信息后,同时注册子和父,并在两者都登录后,子将共享位置,父将收到它。

I would like to add additional fields such as role (parent / child) in my application in React Native + Firebase, to later create other functionalities of the application based on the role.我想在 React Native + Firebase 的应用程序中添加额外的字段,例如角色(父/子),以便稍后根据角色创建应用程序的其他功能。

Registration:登记:

firebase
    .auth()
    .createUserWithEmailAndPassword(email, password)
    .then(userCredentials => {
        return userCredentials
            .user.updateProfile({
                displayName: name,
            })
            .additionalUserInfo.profile = {
            role: role,
        }
    })

Homescreen主屏幕

const { displayName } = firebase.auth().currentUser;
const { role } = firebase.additionalUserInfo.profile;

this.setState({displayName, role});

and role returns undefined.和角色返回未定义。

The properties that you can store on a user profile are defined by Firebase Authentication.您可以存储在用户配置文件中的属性由 Firebase 身份验证定义。 You can't just add additional properties to it as you see fit.您不能只在您认为合适的情况下为其添加其他属性。 At best Firebase will simply ignore those, but likely it will also explicitly reject them (throwing an error). Firebase 充其量只会忽略这些,但它也可能会明确拒绝它们(抛出错误)。

If you want to store additional information about a user, you have two main options:如果要存储有关用户的其他信息,有两个主要选项:

  1. Store the additional information as custom claims in the user's authentication token.将附加信息作为自定义声明存储在用户的身份验证令牌中。
  2. Store the additional information in an external database, such as the Firestore and Realtime Database that are part of Firebase.将附加信息存储在外部数据库中,例如属于 Firebase 的 Firestore 和实时数据库。

While storing the data as custom claims is pretty close to what you want to accomplish, you'll need to keep a few things in mind:虽然将数据存储为自定义声明非常接近您想要完成的工作,但您需要记住以下几点:

  • Custom claims are used for authorization purposes, and for that reason can only be set from a trusted environment (such as your development machine, a server you control, or Cloud Functions).自定义声明用于授权目的,因为这个原因,只能从一个值得信赖的环境设置(如您的开发机,你控制的服务器,或云功能)。 So you can't simply set the role from within the app, and will need a separate process to add that claim.因此,您不能简单地从应用程序中设置role ,并且需要一个单独的过程来添加该声明。
  • After setting a custom claim on a user profile it may take up to an hour before that change is visible in the client.在用户配置文件上设置自定义声明后,最多可能需要一个小时才能在客户端中看到该更改。 If you need it sooner, you can force the user to sign in again, or refresh their ID token.如果您更早需要它,您可以强制用户再次登录,或刷新他们的 ID 令牌。
  • Custom claims are sent with every request you make to Firebase resources, and for that reason are very limited in size.您向 Firebase 资源发出的每个请求都会发送自定义声明,因此大小非常有限。 There's a maximum size of 1000 bytes for custom claims for a user.用户的自定义声明的最大大小为 1000 字节。 While your current role will easily fit into that, it may limit what you can add later.虽然您当前的role很容易融入其中,但它可能会限制您以后可以添加的内容。

If instead you store user data in an external database , you'll typically combine it with other information about that user into a users node/collection.相反,如果您将用户数据存储在外部数据库中,您通常会将其与有关该用户的其他信息组合到users节点/集合中。 In here you'd store a document/node for each user based on that user's UID, and then their profile information.在这里,您将根据该用户的 UID 以及他们的个人资料信息为每个用户存储一个文档/节点。

So something like:所以像:

users: {
  uidOfAleksandra: {
    username: "Aleksandra",
    displayName: "Aleksandra Lastname",
    role: "parent",
    registrationDate: "2020-02-01"
  },
  uidOfPuf: {
    username: "puf",
    displayName: "Frank van Puffelen",
    role: "child",
    registrationDate: "2015-03-07"
  },
}

Having this list of user profiles does not only allow you to store the additional information for each user, but would also allow you to query that list of users from within your app, something that the Authentication API doesn't allow from within application code.拥有此用户配置文件列表不仅允许您存储每个用户的附加信息,而且还允许您从应用程序内查询该用户列表,这是身份验证 API 不允许从应用程序代码内进行的操作。

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

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