我有这样的代码来登录用户并将其保存到Firebase数据库
async EmailRegister(email: any, password: any, displayName: any) {
return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
.then((result) => {
result.user.updateProfile({
displayName: displayName,
photoURL: ''
}).then(() => {
this.updateUserData(result.user);
// this.SendVerificationMail();
this.router.navigateByUrl('/pages/projects/all-project');
window.alert("You have been successfully registered!");
console.log(result.user)
});
}).catch((error) => {
window.alert(error.message)
})
}
private updateUserData(user) {
// Sets user data to firestore on login
const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);
const data = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL,
emailVerified: user.emailVerified,
kewirusType: user.kewirusType
}
return userRef.set(data, { merge: true })
}
我想像kewirusType
用户注册时一样传递新数据,该怎么做?
要解决您的问题,请尝试将kewirusType
from表单传递给updateUserData,例如:
async EmailRegister(email: any, password: any, displayName: any, kewirusType: string) {
return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
.then((result) => {
result.user.updateProfile({
displayName: displayName,
photoURL: ''
}).then(() => {
this.updateUserData(result.user, kewirusType);
// this.SendVerificationMail();
this.router.navigateByUrl('/pages/projects/all-project');
window.alert("You have been successfully registered!");
console.log(result.user)
});
}).catch((error) => {
window.alert(error.message)
})
}
updateUserData
:private updateUserData(user, kewirusType: string) {
// Sets user data to firestore on login
const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);
const data = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL,
emailVerified: user.emailVerified,
kewirusType //notice change here
}
return userRef.set(data, { merge: true })
}
额外:
可以考虑使用Firebase函数来创建user/{uuid}
文档,而不是在您的应用程序中创建此数据,例如:
export const initUserData = functions.auth.user().onCreate((user) => {
const collectionDoc = admin.firestore().collection(`users`);
return collectionDoc.doc(user.uid).set({
uid: user.uid,
email: user.email,
kewirusType: "value"
//... your data here
}).then(() => {
console.log(`Data for user ${user.uid} initialized`);
});
});
然后,在您的应用中,您只需订阅/user/{uuid}
文档即可获取用户数据。在应用程序外部委派此类过程更好,更安全。
Firebase功能可以访问每个firebase服务(身份验证,文档,存储等),因此基本上您可以使用它做很多事情。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句