Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Profile Details Using Angularfire 2 + Firebase 3.x #281

Closed
dorcilio opened this issue Jun 24, 2016 · 14 comments
Closed

Update Profile Details Using Angularfire 2 + Firebase 3.x #281

dorcilio opened this issue Jun 24, 2016 · 14 comments

Comments

@dorcilio
Copy link

Hi!

I would like to include more information in the profile, with angularfire2 is possible? how to proceed? In the case without the Google authentication using only email and password ...

Thanks in advance!

@buoyad
Copy link
Contributor

buoyad commented Jun 27, 2016

I think the way to go about this is to store extra profile data in a normal firebase database with the associated username/UID.

@davideast
Copy link
Member

@dorcilio Like @buoyad said, you need store this information in your database. Authentication and Database are separate but integrated services.

@maxmumford
Copy link
Contributor

From the docs it looks like firebase has fields on the auth object for a user's displayName and profile photo etc:

https://firebase.google.com/docs/auth/web/manage-users

Are you planning on including this in the angularfire2 lib?

@burtonjc
Copy link

burtonjc commented Oct 29, 2016

Looks like you can get at through FirebaseAuthState:

authState.auth.updateProfile({
  displayName: 'display name',
  photoURL: 'some/url'
}).then(() => {
  ...
});

The typings are a little strict on it if you are using typescript. So you have to pass both displayName and photoUrl; you can pass them as null though if you don't want to set one.

@psaussure
Copy link

@burtonjc from where can you call this? if in my modification class i have
constructor(public af: AngularFire)

where on af instance can I retrieve authState without subcribing with this.af.auth.subscribe(authState => { }) (which you call at login for example).

@burtonjc
Copy link

burtonjc commented Dec 5, 2016

Because that value is wrapped in an observable I think you have to subscribe to get it. You might be able to do something fancy with BehaviorSubjects but I'm not sure.

To avoid subscribing to auth states all over the place, you could create a service to manage the subscription:

import { Injectable } from '@angular/core';
import { FirebaseAuth, FirebaseAuthState } from 'angularfire2';

@Injectable()
export class AuthService {
  private _authState: FirebaseAuthState = null;

  constructor(public auth: FirebaseAuth) {
    this.auth.subscribe((state: FirebaseAuthState) => {
      this._authState = state;
    });
  }

  get authenticated(): boolean {
    return this._authState !== null;
  }

  get authState(): FirebaseAuthState {
    return this._authState;
  }
}

The danger there is if you call AuthService#authState before the subscription has fired, you will get a null value.

Hope that helps!

@hemanrnjn
Copy link

@psaussure I am facing the exact same problem as you are asking. Did you get it to work? If yes, then how? Please help.

@andres-gonzalez
Copy link

andres-gonzalez commented May 17, 2017

I think the best way to update the profile is using native firebase, just import it and use it like this:

import * as firebase from 'firebase';

ionViewDidLoad() {
    firebase.auth().onAuthStateChanged(function (user) {
      user.updateProfile({
        displayName: 'Pepe Pecas'
      })      
      if (user) {
        console.log(user)
      } else {
      
      }
    });
  }

@mitCode
Copy link

mitCode commented Jul 20, 2017

updateProfile is part of auth and not authState.
Try this:
auth.currentUser.updateProfile

@Sanjay-Ghadge
Copy link

Sanjay-Ghadge commented Dec 17, 2017

How to update phone number of user in angularfire2?
How do I get phoneCredential?

@crobbs
Copy link

crobbs commented Apr 25, 2018

Before you try, make sure you have recently logged in. Try to log out and then log in...then run another test.

@ralyodio
Copy link

I get an error that auth.currentUser doesn't exist.

@phc15
Copy link

phc15 commented Jul 31, 2018

use firebase.auth().currentUser

@ronyef
Copy link

ronyef commented Jan 10, 2019

this.afAuth.auth.currentUser.updateProfile({
          displayName: this.name,
          photoURL: ''
        }).then(() => {
          console.log('DisplayName updated')
        }).catch(err => console.log(err))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests