如何在 AngularFire2 中获取 firebase.User [英] How to get the firebase.User in AngularFire2
问题描述
我正在使用 AngularFire2 (Ionic2) 和 Firebase 身份验证.
我在尝试获取当前用户时遇到问题.这对我有用,但不一致.有时填充,有时为空.
让用户:firebase.User = firebase.auth().currentUser;
有没有办法更一致地获取当前用户?
更多信息:
我的登录方式如下:
import { FirebaseAuth, AuthProviders, AuthMethods } from 'angularfire2';公共身份验证:FirebaseAuththis.auth.login({提供者:AuthProviders.Google,方法:AuthMethods.Popup}).then((data: any) => {this.fireAuth.onAuthStateChanged((user) => {....});});
这里总是填充用户.但是我需要访问用户的地方是代码的另一部分.
感谢任何帮助.
更新
我尝试使用以下方法获取当前用户:
this.auth.subscribe((authData) => {让用户 = authData.auth;凭据 = firebase.auth.GoogleAuthProvider.credential(googleIdToken, googleAccessToken);user.reauthenticate(credential).then(() => {...});
但是当我尝试创建凭据时,出现以下错误:
<块引用>例外:未捕获(承诺):错误:重新验证失败:首先参数凭据"必须是有效凭据.
当我在第一种方法中获取当前用户时,我没有收到任何错误并且可以正常工作.
更多信息:
当我 console.log
以下 user
对象时,它们看起来完全相同:
让用户:firebase.User = this.fireAuth.currentUser;控制台日志(用户);
和
this.auth.subscribe((authData) => {让用户:firebase.User = authData.auth;控制台日志(用户);});
但一个有效(当不为空时)而另一个无法重新验证.
注意有一个新的 AngularFire 版本.(4.0.0)
在新版本中,所有模块都被拆分了.这意味着,您需要使用 AngularFireAuth
希望能帮到你
I am using AngularFire2 (Ionic2) and Firebase Authentication.
I am having a problem trying to get the current user. The works for me, but inconsistently. Sometimes it is populated, and sometimes it is null.
let user: firebase.User = firebase.auth().currentUser;
Is there a way to get the current user more consistently?
More Info:
I am logging in as follows:
import { FirebaseAuth, AuthProviders, AuthMethods } from 'angularfire2';
public auth: FirebaseAuth
this.auth.login({
provider: AuthProviders.Google,
method: AuthMethods.Popup
}).then((data: any) => {
this.fireAuth.onAuthStateChanged((user) => {
....
});
});
Here the user is always populated. But where I need to get access to the user, is in another part of the code.
Any help appreciated.
UPDATE
I try use the following to get the current user:
this.auth.subscribe((authData) => {
let user = authData.auth;
credential = firebase.auth.GoogleAuthProvider.credential(googleIdToken, googleAccessToken);
user.reauthenticate(credential).then(() => {...
});
But then when I try create the credentials, I get the following error:
EXCEPTION: Uncaught (in promise): Error: reauthenticate failed: First argument "credential" must be a valid credential.
When I get the current user in the first method, I don't get any errors and it works.
More info:
When I console.log
the following user
objects, they both look identical:
let user: firebase.User = this.fireAuth.currentUser;
console.log(user);
and
this.auth.subscribe((authData) => {
let user: firebase.User = authData.auth;
console.log(user);
});
but the one works (when not null) and the other cannot reauthenticate.
Attention there is a new AngularFire Version. (4.0.0)
In the new version all the modules are split up. That means, you need to use the AngularFireAuth
constructor(afAuth: AngularFireAuth) { afAuth.authState.subscribe(auth => { if(auth) { console.log('logged in'); } else { console.log('not logged in'); } }); }
Hope I can help
这篇关于如何在 AngularFire2 中获取 firebase.User的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!