1

I've seen this asked a few ways but I cannot make heads or tails of the solutions provided. Can you help me out and let me know how I can push additional user fields into Firebase so I can recall them later? Here is what I currently have going on:

    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    import { GoogleAnalytics } from '@ionic-native/google-analytics';
    import { UserData } from '../../providers/user-data';
    import { User } from '../../interfaces/user-options';
    import { AngularFireAuth } from "angularfire2/auth";
    import { AngularFireDatabase } from 'angularfire2/database';
    import { LoginPage } from "../login/login";
    import { Storage } from '@ionic/storage';

    @Component({
      selector: 'page-user',
      templateUrl: 'signup.html'
    })
    export class SignupPage {

      user = {} as User;

      constructor(
        private afAuth: AngularFireAuth,
        private afDb: AngularFireDatabase,
        public navCtrl: NavController, 
        public userData: UserData,
        public storage: Storage,
        private ga: GoogleAnalytics,
      ) {}

      ionViewDidLoad() {
        var trackingId = 'UA-114720506-2';
        if (/(android)/i.test(navigator.userAgent)) { // for android 
          trackingId = 'UA-114720506-2';
        } else if (/(ipod|iphone|ipad)/i.test(navigator.userAgent)) { // for ios
          trackingId = 'UA-114720506-2';
        }      

        this.ga.debugMode();
        this.ga.startTrackerWithId(trackingId).then(()=> {
          console.log("GoogleAnalytics About Initialized with: " + trackingId);
          this.ga.trackView('Signup Screen');
          this.ga.enableUncaughtExceptionReporting(true)
          .then((_success) => {
            console.log("GoogleAnalytics enableUncaughtExceptionReporting Enabled.");
          }).catch((_error) => {
            console.log("GoogleAnalytics Error enableUncaughtExceptionReporting : " + _error)
          });
        });    

      }  

      async register(user: User){
        try {
          const result = await this.afAuth.auth.createUserWithEmailAndPassword(user.email, user.password)
            .then(
                (success) => {
                  console.log(result); 
                  success.updateProfile({
                    displayName: user.displayName,
                    photoURL: "https://images-na.ssl-images-amazon.com/images/I/41Ee94UeiAL._SY355_.jpg"
                  })

                  this.ref.child(user.uid).set(user).then
                  this.navCtrl.setRoot(LoginPage);
                });
            }
            catch(e) {
              console.error(e);
            }  
        }

    }

      ref() {
        this.afDb.database().ref().child(user);
      }

Here is my user data

    export interface User {
      displayName: string,
      email: string,
      password: string,
      company: string,
      phoneNumber: string,
      position: string,
    }

This is clearly not working. Can someone help out? Thanks!

javapatriot
  • 353
  • 4
  • 15
  • Possible duplicate of [How do I link each user to their data in Firebase?](https://stackoverflow.com/questions/30910704/how-do-i-link-each-user-to-their-data-in-firebase) – André Kool Apr 04 '18 at 08:17

1 Answers1

0

You are not being specific as to what exactly is wrong and why the code doesn't work, but one way to keep more info about the user on top of what Firebase saves is to create a new branch in the database, and store the user info there, i.e.:

/userDetails/${uid}/

Then, assuming userData is an object storing data about the user, you can use update or set:

firebase.database().ref("userDetails/"+user.uid).set(userData)

Couple of side notes

-Never store user's password (store its hashed value instead). Just Google the subject

-Don't use Angular Fire if you don't have to. In the long run, you will have to be worried about its compatibility and maintenance. Firebase web sdk has everything you need.

Ari
  • 7,251
  • 11
  • 40
  • 70