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

时间:2016-07-11 14:40:11

标签: typescript angular firebase angularfire firebase-authentication

你能帮帮我吗?我无法在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.
}

2 个答案:

答案 0 :(得分:3)

您可以在示例应用部分中看到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')
        }

    });
  }
}

答案 1 :(得分:1)

当您使用 Angularfire2 时,这应该是这样的:

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/