向注册用户Firebase添加额外信息 [英] Adding extra information to registration user Firebase

查看:33
本文介绍了向注册用户Firebase添加额外信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在React Native中创建一个应用程序,该应用程序可以在父模式和子模式两种模式下工作.初始阶段是向Firebase注册,然后添加有关角色(父/子)的其他信息,同时注册子代和父代,并且在登录双方后,子代将共享位置,父代将收到位置.

我想在React Native + Firebase的应用程序中添加其他字段,例如角色(父/子),以便稍后基于角色创建应用程序的其他功能.

注册:

  firebase.auth().createUserWithEmailAndPassword(电子邮件,密码).then(userCredentials => {返回userCredentials.user.updateProfile({displayName:名称,}).additionalUserInfo.profile = {角色:}}) 

主屏幕

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

并且角色返回未定义.

解决方案

您可以存储在用户配置文件中的属性由Firebase身份验证定义.您不能随便在其上添加其他属性.最好的情况是,Firebase只会忽略这些内容,但很可能也会明确拒绝它们(引发错误).

如果要存储有关用户的其他信息,则有两个主要选项:

  1. 将其他信息作为自定义声明存储在用户的身份验证令牌中.
  2. 将其他信息存储在外部数据库中,例如Firebase的Firestore和实时数据库.

虽然将数据存储为自定义声明与您要完成的目标非常接近,但您需要牢记以下几点:

  • 自定义声明用于授权目的,因此只能在受信任的环境(例如您的开发计算机,您控制的服务器或Cloud Functions)中设置.因此,您不能简单地在应用程序内设置 role ,并且需要一个单独的过程来添加该声明.
  • 在用户个人资料上设置自定义声明后,最多可能需要一个小时才能在客户端看到该更改.如果需要更快,则可以强制用户再次登录或刷新其ID令牌.
  • 自定义声明随您对Firebase资源的每个请求一起发送,因此大小非常有限.用户的自定义声明的最大大小为1000字节.尽管您当前的角色可以轻松地适应其中,但可能会限制您以后添加的内容.

相反,如果您将用户数据存储在外部数据库中,则通常会将其与有关该用户的其他信息合并到 users 节点/集合中.在此处,您将根据该用户的UID,然后是其个人资料信息,为每个用户存储一个文档/节点.

所以像这样:

 用户:{uidOfAleksandra:{用户名:"Aleksandra",displayName:"Aleksandra Lastname",角色:父母",注册日期:"2020-02-01"},uidOfPuf:{用户名:"puf",displayName:"Frank van Puffelen",角色:孩子",注册日期:"2015-03-07"},} 

拥有此用户配置文件列表不仅可以让您存储每个用户的其他信息,而且还可以让您从应用程序内查询该用户列表,而Authentication API不允许在内部进行此操作应用程序代码.

I would like to make an application in React Native that allows to work in two modes, parent and child. 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.

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.

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. 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).

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.

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.
  • 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.
  • Custom claims are sent with every request you make to Firebase resources, and for that reason are very limited in size. There's a maximum size of 1000 bytes for custom claims for a user. While your current role will easily fit into that, it may limit what you can add later.

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. In here you'd store a document/node for each user based on that user's UID, and then their profile information.

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.

这篇关于向注册用户Firebase添加额外信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆