Ionic Firebase AngularFire2 User roles and database


#1

Hi guys,

I’m currently developing an app that requires two user types, lets say Leaders and Employees. I’d like to make use of Firebase authentication and Firestore. I have Firebase authentication working but I can’t seem to map the userID Firebase authentication generates to my User collection in my database.

I’d like to have the user sign up with Email and password, then for the userID, the email and some other details to be saved to the User collection in the database.

I currently have this in my auth provider:

export class AuthProvider {
  
  user: Observable<firebase.User>;
  userID = firebase.auth().currentUser.uid;

  constructor(private afAuth: AngularFireAuth) {
    this.user = afAuth.authState;
  }

  registerUser(email: string, password: string) {
    this.afAuth
      .auth
      .createUserWithEmailAndPassword(email, password)
      .then(user => {
        firebase.firestore().collection('users').doc(this.userID).set(user);
        console.log('Success, user created', user);
      })
      .catch(err => {
        console.log('Something went wrong:',err.message);
      });    
  }

They are getting added but I can’t get the “uid” to add to the collection in the database due to the “uid” being null. I believe I have to get the “uid” when the user is created but I’m unsure how.

If anyone could help and also point me in the direction of good tutorials involving Ionic and Firebase/Firestore it would be appreciated.

Thanks.


#2

This is much simpler with Angular Firestore. (angularfire2)


#3

Can’t you see I’m trying to use that?


#4

Why are you mixing idioms then?


#5

Sorry, I’m new. I trying to figure out how to do it from many sources. Recording to the database is what I’m stuck on. I still use Firebase auth to signup and login right?


#6

This is the first line of the AngularFire auth documentation.

AngularFireAuth.authState provides you an Observable<firebase.User> to monitor your application’s authentication State.


#7

https://javebratt.com/ionic-firebase-authentication/

This is a couple years old, but is a great place to start.
The firebase docs, or angularFire docs as @AaronSterling points out, are where you want to end up but for someone new they’re pretty intense and more confusing than they are helpful (at least they were for me)