naoval
naoval

Reputation: 368

Add custom variable/data to AngularFirestoreDocument

I have a code like this to login user and save them to firebase database

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

I want to pass new data like kewirusType when user registers, how to do that?

Upvotes: 1

Views: 245

Answers (1)

Beniamin
Beniamin

Reputation: 568

To just fix your problem try pass the kewirusType from form to updateUserData for example:

  1. pass the kewirus to this method:
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)
    })
}
  1. and to 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 })
}

EXTRA:

Instead of create this data in your app, consider using firebase functions to create user/{uuid} document, for example:


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`);
  });

});

Then in your app you can simply subscribe /user/{uuid} document to get user's data. Its better and safer to delegate such processes outside the app.

Firebase function have access to each firebase service (auth, documents, storage etc) so basically you can do a lot with it.

Upvotes: 1

Related Questions