xcode 将自定义属性添加到 Firebase 身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37825520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Custom Attributes to Firebase Auth
提问by Dan Levy
I have hunted through Firebase's docs and can't seem to find a way to add custom attributes to FIRAuth. I am migrating an app from Parse-Server and I know that I could set a user's username, email, and objectId. No I see that I have the option for email, displayName, and photoURL. I want to be able to add custom attributes like the user's name. For example, I can use:
我已经搜索了 Firebase 的文档,但似乎找不到将自定义属性添加到 FIRAuth 的方法。我正在从 Parse-Server 迁移一个应用程序,我知道我可以设置用户的用户名、电子邮件和 objectId。不,我看到我可以选择 email、displayName 和 photoURL。我希望能够添加用户名等自定义属性。例如,我可以使用:
let user = FIRAuth.auth()?.currentUser
if let user = user {
let changeRequest = user.profileChangeRequest()
changeRequest.displayName = "Jane Q. User"
changeRequest.photoURL =
NSURL(string: "https://example.com/jane-q-user/profile.jpg")
changeRequest.setValue("Test1Name", forKey: "usersName")
changeRequest.commitChangesWithCompletion { error in
if error != nil {
print("\(error!.code): \(error!.localizedDescription)")
} else {
print("User's Display Name: \(user.displayName!)")
print("User's Name: \(user.valueForKey("name"))")
}
}
}
When I run the code, I get an error that "usersName" is not key value compliant. Is this not the right code to use. I can't seem to find another way.
当我运行代码时,出现“usersName”不符合键值的错误。这不是要使用的正确代码吗?我似乎找不到其他方法。
回答by Fred Dupray
You can't add custom attributes to Firebase Auth. Default attributes have been made available to facilitate access to user information, especially when using a provider (such as Facebook).
您无法向 Firebase 身份验证添加自定义属性。已提供默认属性以方便访问用户信息,尤其是在使用提供商(例如 Facebook)时。
If you need to store more information about a user, use the Firebase realtime database. I recommend having a "Users" parent, that will hold all the User children. Also, have a userId key or an email key in order to identify the users and associate them with their respective accounts.
如果您需要存储有关用户的更多信息,请使用 Firebase 实时数据库。我建议有一个“用户”父级,它将容纳所有用户子级。此外,还有一个 userId 密钥或电子邮件密钥,以便识别用户并将他们与各自的帐户相关联。
Hope this helps.
希望这可以帮助。
回答by Vitaliy A
See how to add custom claims to auth info here: Firebase Auth with custom claimsKeep in mind that it's limited to 1000 bytes
在此处查看如何向身份验证信息添加自定义声明: Firebase Auth with custom claim请记住,它限制为 1000 字节
回答by Xiaoqi Hu
Firebase Auth users don't have "usersName" attribute so the code got error.
Firebase Auth 用户没有“usersName”属性,因此代码出错。
Please check the doc: Firebase Auth user infoand how to change attributes.
请检查文档:Firebase Auth 用户信息和如何更改属性。
回答by Michael McQuade
While in most cases you cannot add custom information to a user, there are cases where you can.
虽然在大多数情况下您无法向用户添加自定义信息,但在某些情况下您可以。
If you are creating or modifying users using the Admin SDK, you may create custom claims. These custom claims can be used within your client by accessing attributes of the claims object.
如果您使用 Admin SDK 创建或修改用户,您可以创建自定义声明。通过访问声明对象的属性,可以在您的客户端中使用这些自定义声明。
Swift code from the Firebase documentation:
Firebase文档中的Swift 代码:
user.getIDTokenResult(completion: { (result, error) in
guard let admin = result?.claims?["admin"] as? NSNumber else {
// Show regular user UI.
showRegularUI()
return
}
if admin.boolValue {
// Show admin UI.
showAdminUI()
} else {
// Show regular user UI.
showRegularUI()
}
})
Node.js code for adding the claim:
用于添加声明的 Node.js 代码:
// Set admin privilege on the user corresponding to uid.
admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
});