我已尝试过此代码,但它无效。我使用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.
}
答案 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/