除了 firebase 中的电子邮件和密码外,如何添加用户的额外个人资料信息?
how to add user's extra profile information apart from email and password in firebase?
我在 angular4 和 angularfire2 库中使用 firebase。使用电子邮件和密码注册用户后,我可以看到用户已在 firebase 的数据库中注册。但是我不能添加额外的字段,如姓名、地址等。我想创建一个 ('/users') 列表,然后使用用户的 Uid 来存储用户个人资料信息。但是文档说我可以分配 "name" 和 "profile picture"。如果是这种情况,我无法通过身份验证向用户数据库添加任何字段。
正如您提到的,根据 firebase 文档,您可以像这样使用 angularfire2 来做到这一点:
this.af.auth.createUser({
email: <UserEmail>,
password: <UserPass>
}).then(
(success) => {
success.auth.updateProfile({
displayName: <UserName>,
photoURL: <UserPhotoURLString>
})
.then(res => console.log("profile updated"))
.catch(err => console.log(err));
}).catch(
(err) => {
console.log(err);
})
正如您在上面的代码附件中看到的,在您使用 af.auth.createUser
的内置方法后,您会收到一个 User 对象作为承诺,之后您可以使用另一个内置方法 success.auth.updateProfile
更新 firebase 身份验证部分中的用户配置文件区域。
这段代码对我来说非常有用,所以只需将其调整为您的代码即可:)享受吧。
AngularFire2有新版本更新。您可以为新的 angularfire 版本试用此代码。
this.afAuth.auth.currentUser.updateProfile({
displayName: 'your profile name',
photoURL: 'your pic url'
}).then(() => {
console.log('DisplayName updated')
}).catch(err => console.log(err))
您可以在登录时使用此代码更新用户日期。
我花了一段时间才弄明白这个问题,但我找不到最新 firebase 版本的任何最新答案。所以我希望这个解决方案也适合你。
我的诀窍是我必须在更新配置文件上添加一个 await
并使函数 async
.
这样做的好处是您可以使用可以存储在 localStorage
或 sessionStorage
中的 firebase 用户对象,并且您可以轻松地从那里引用您需要的信息,而不是创建您总是需要查询的新文档。
SignUp(user, password) {
return this.afAuth.createUserWithEmailAndPassword(user.email, password)
.then(async (result) => {
/* Call the SendVerificaitonMail() function when new user sign
up and returns promise */
this.SendVerificationMail();
await result.user?.updateProfile({
displayName: user.name+ ' '+ user.surname,
photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(() => {
this.SetUserData(result.user, user);
this.ngZone.run(() => {
this.router.navigate(['home']);
});
}).catch((error) => {
// An error occurred
// ...
});
}).catch((error) => {
window.alert('Something wrong with Signup '+error.message);
});
}
我在 angular4 和 angularfire2 库中使用 firebase。使用电子邮件和密码注册用户后,我可以看到用户已在 firebase 的数据库中注册。但是我不能添加额外的字段,如姓名、地址等。我想创建一个 ('/users') 列表,然后使用用户的 Uid 来存储用户个人资料信息。但是文档说我可以分配 "name" 和 "profile picture"。如果是这种情况,我无法通过身份验证向用户数据库添加任何字段。
正如您提到的,根据 firebase 文档,您可以像这样使用 angularfire2 来做到这一点:
this.af.auth.createUser({
email: <UserEmail>,
password: <UserPass>
}).then(
(success) => {
success.auth.updateProfile({
displayName: <UserName>,
photoURL: <UserPhotoURLString>
})
.then(res => console.log("profile updated"))
.catch(err => console.log(err));
}).catch(
(err) => {
console.log(err);
})
正如您在上面的代码附件中看到的,在您使用 af.auth.createUser
的内置方法后,您会收到一个 User 对象作为承诺,之后您可以使用另一个内置方法 success.auth.updateProfile
更新 firebase 身份验证部分中的用户配置文件区域。
这段代码对我来说非常有用,所以只需将其调整为您的代码即可:)享受吧。
AngularFire2有新版本更新。您可以为新的 angularfire 版本试用此代码。
this.afAuth.auth.currentUser.updateProfile({
displayName: 'your profile name',
photoURL: 'your pic url'
}).then(() => {
console.log('DisplayName updated')
}).catch(err => console.log(err))
您可以在登录时使用此代码更新用户日期。
我花了一段时间才弄明白这个问题,但我找不到最新 firebase 版本的任何最新答案。所以我希望这个解决方案也适合你。
我的诀窍是我必须在更新配置文件上添加一个 await
并使函数 async
.
这样做的好处是您可以使用可以存储在 localStorage
或 sessionStorage
中的 firebase 用户对象,并且您可以轻松地从那里引用您需要的信息,而不是创建您总是需要查询的新文档。
SignUp(user, password) {
return this.afAuth.createUserWithEmailAndPassword(user.email, password)
.then(async (result) => {
/* Call the SendVerificaitonMail() function when new user sign
up and returns promise */
this.SendVerificationMail();
await result.user?.updateProfile({
displayName: user.name+ ' '+ user.surname,
photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(() => {
this.SetUserData(result.user, user);
this.ngZone.run(() => {
this.router.navigate(['home']);
});
}).catch((error) => {
// An error occurred
// ...
});
}).catch((error) => {
window.alert('Something wrong with Signup '+error.message);
});
}