更新时间:2023-12-06 13:12:40
您可以存储在用户配置文件中的属性由Firebase身份验证定义.您不能随便在其上添加其他属性.***的情况是,Firebase只会忽略这些内容,但很可能也会明确拒绝它们(引发错误).
如果要存储有关用户的其他信息,则有两个主要选项:
虽然将数据存储为自定义声明与您要完成的目标非常接近,但您需要牢记以下几点:
role
,并且需要一个单独的过程来添加该声明.角色
可以轻松地适应其中,但可能会限制您以后添加的内容.相反,如果您将用户数据存储在外部数据库中,则通常会将其与有关该用户的其他信息合并到 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:
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:
role
from within the app, and will need a separate process to add that claim.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.