应该没有理由不能使用底层的Firebase API,因为由AngularFire2创建的Firebase App可用于注入。
您可以使用注入的应用程序访问未由AngularFire2公开的auth()
(或storage()
)实例的任何方法。
举一个例子的目的,你可以把它注射到一个应用程序组件是这样的:
import { Component, Inject } from "@angular/core";
import { AngularFire, FirebaseApp } from "angularfire2";
import * as firebase from "firebase";
@Component({
selector: "app",
template: ...
})
export class AppComponent {
constructor(
// Inject AngularFire2:
private angularFire: AngularFire,
// Inject the Firebase App instance:
@Inject(FirebaseApp) private firebaseApp: firebase.app.App
) {
// Perform some sort of login.
...
angularFire.auth.subscribe((state) => {
// AngularFire2's auth observable will emit when the authentication
// state changes and if the state is non-null, there will be a
// current user. At this point, you should be able to do with the
// injected app what it was you were doing with the SDK.
if (state) {
var credential = firebase.auth
.GoogleAuthProvider
.credential(googleUser.getAuthResponse().id_token);
firebaseApp.auth()
.currentUser
.link(credential)
.then((user) => {
console.log("Anonymous account successfully upgraded", user);
})
.catch((error) => {
console.log("Error upgrading anonymous account", error);
});
}
});
}
}
您不必使用auth
观察的;您还可以使用AngularFire2的auth.login
方法返回的承诺将代码放入承诺链中。
正如以下评论所述,GoogleAuthProvider
类位于firebase.app
命名空间(不是应用程序实例)中。另外,由于当前用户可以通过FirebaseAuthState
(易混淆命名)的auth
属性中的AngularFire2提供,因此您无需注入应用实例即可执行您想要对Google提供商执行的操作。您只需要输入firebase
:
this.angularFire.auth.login({
email: "[email protected]",
password: "password",
})
.then((authState: FirebaseAuthState) => {
authState.auth.linkWithRedirect(new firebase.auth.GoogleAuthProvider());
});
它看起来不像您可以从应用程序的身份验证服务访问GoogleAuthProvider类。 – DenimChicken
它看起来像我混淆了一个名称空间的实例。尝试'firebase.auth.GoogleAuthProvider' - 其中'firebase'是示例中导入的命名空间。 (如果解决了这个问题,我会重写答案,因为不需要注入'FirebaseApp')。 – cartant
我能够像这样升级一个帐户:'this.firebaseApp.auth()。 currentUser.linkWithRedirect(new firebase.auth.GoogleAuthProvider());'感谢您指点我正确的方向! – DenimChicken