Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UpdateProfile() during singup Firebase

I'm working on an app and I want to achieve this. When an user signing in I want to get his username and store in firebase. So I created this :

Signup(email: string, password: string, displayName: string) {
    this.angFire.auth.createUser({
      email: email,
      password: password
    }).catch(
      (err) => {
        console.log(err);
        this.error = err;
      })
...

And this works, now I need to save also displayName into current user, so I try to do this inside the same function (Singup()) :

let user = firebase.auth().currentUser;
    user.updateProfile({
      displayName: this.displayName,
      photoURL: "https://example.com/jane-q-user/profile.jpg"
    }).then(function() {
      console.log("Nome utente inserito");
    }, function(error) {
      console.log("Errore durante inserimento utente");
    });

But it gives me this :

error_handler.js:54 EXCEPTION: Error in ./SignupPage class SignupPage - inline template:70:4 caused by: Cannot read property 'updateProfile' of null

How to do it? I'm running on ionic 2 angular2, firebase/angulrfire

Doc ref :

Update a user's profile

You can update a user's basic profile information—the user's display name and profile photo URL—with the updateProfile method. For example:

var user = firebase.auth().currentUser;

user.updateProfile({ displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg" }).then(function() {
// Update successful. }, function(error) { // An error happened. });

Here

like image 250
Trip Therapy Avatar asked Mar 21 '17 14:03

Trip Therapy


People also ask

What does Firebase auth () CurrentUser return?

If a user isn't signed in, CurrentUser returns null. Note: CurrentUser might also return null because the auth object has not finished initializing. If you use a listener to keep track of the user's sign-in status, you don't need to handle this case.

How do you check if a user is authenticated in Firebase?

You can easily detect if the user is logged or not by executing: var user = firebase. auth().


1 Answers

My answer is way too late but just in case someone is interested, you could do it like this

  constructor(
    private afAuth: AngularFireAuth,
    private router: Router
  ) { 
   //
  }


async signupUser(email: string, password: string): Promise<any> {
    const data = await this.afAuth.createUserWithEmailAndPassword(
      email,
      password
    );
    return this.updateUserData(data.user).then(() => {
      this.router.navigate(["/"]);
    });
  }

 private updateUserData({ uid, email, photoURL, displayName }: User) {
    const userRef: AngularFirestoreDocument<User> = this.afs.doc(
      `users/${uid}`
    );
    return userRef.set({ uid, email, photoURL, displayName }, { merge: true });
 }

You can learn more here

like image 154
brijmcq Avatar answered Sep 19 '22 12:09

brijmcq