使用 Angular2 在 Firebase 中获取用户身份验证配置文件信息

Get User Auth Profile Info in Firebase using Angular2

你能帮帮我吗?我无法在 Angularfire 身份验证中获取用户的个人资料信息,例如他们的 facebook 个人资料图片或 facebook 名称。请帮忙。非常感谢!

我试过这段代码,但它不起作用。我使用 Angular 2 TypeScript。

var user = firebase.auth().currentUser;
var name, email, photoUrl, uid;

if (user != null) {
  name = user.displayName;
  email = user.email;
  photoUrl = user.photoURL;
  uid = user.uid;  // The user's ID, unique to the Firebase project. Do NOT use
                   // this value to authenticate with your backend server, if
                   // you have one. Use User.getToken() instead.
}

当您使用 A​​ngularfire2 时,应该是这样的:

constructor(public af: AngularFire) {
    this.af.auth.subscribe(auth => {
     //Here you get the user information
     console.log(auth));
    }
  }
  login() {
    this.af.auth.login({
      provider: AuthProviders.Facebook,
      method: AuthMethods.Popup,
    });
  }

查看此处了解更多信息:https://angularfire2.com/api/

正如您在 示例应用 部分中看到的 here ,您可以使用 auth.subscribe 检测身份验证状态:

export class myClass {
  constructor(public af: AngularFire) {
    this.af.auth.subscribe(auth => {
        if(auth) {
            console.log('You are authenticated', auth)
        } else {
            console.log('You are not authenticated')
        }

    });
  }
}