javascript - result.user.link 不是 Angular Firebase 中的函数

标签 javascript angular firebase-authentication

无法将 Facebook 帐户与现有的 Firebase 帐户关联。我目前有一个使用 google 凭据创建的 firebase 帐户。现在我想将 Facebook 帐户与此现有的 firebase 帐户链接(两者都具有相同的凭据),并且我按照此处提到的步骤进行操作: https://firebase.google.com/docs/auth/web/facebook-login

但最后,当我调用方法“result.user.link(pendingCred).then( (user) => { .. }”来链接帐户时,我在控制台中收到以下错误:

Uncaught TypeError: result.user.link is not a function
at auth.service.ts:188
at e.g (auth.js:23)
at Yb (auth.js:26)
at Ub (auth.js:26)
at z.webpackJsonp.../../../../@firebase/auth/dist/auth.js.h.Mb (auth.js:25)
at Cb (auth.js:19)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:392)
at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run (zone.js:142)
at zone.js:873
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:425)

这是我的代码

loginFacebook(): Promise<any> {
    return this.afAuth.auth.signInWithPopup(new firebase.auth.FacebookAuthProvider())
    .then( (result) => {
        this.registerUserName = result.user.displayName;
        this.authDbUsername = result.user.email.replace('.', '');
        // this.setLoggedinUser(user)
    })
    .catch ( (error) => {
        if (error.code === 'auth/account-exists-with-different-credential') {
            alert('You have already signed up with a different auth provider for that email.');
            const pendingCred = error.credential;
            // The provider account's email address.
            const email = error.email;
            // Get registered providers for this email.
            firebase.auth().fetchProvidersForEmail(email).then( (providers) => {
                // Step 3.
                // If the user has several providers,
                // the first provider in the list will be the "recommended" provider to use.
                if (providers[0] === 'password') {
                    // Asks the user his password.
                    // In real scenario, you should handle this asynchronously.
                    const password = this.promptUserForPassword(providers[0]); // TODO: implement promptUserForPassword.
                    firebase.auth().signInWithEmailAndPassword(email, password).then( (user)  => {
                        // Step 4a.
                        return user.link(pendingCred);
                    }).then(function( user) {
                        // Google account successfully linked to the existing Firebase user.
                        this.authState = user;
                    });
                    return;
                }
                // All the other cases are external providers.
                // Construct provider object for that provider.
                // TODO: implement getProviderForProviderId.
                const provider = new firebase.auth.GoogleAuthProvider(); // this.getProviderForProviderId(providers[0]);
                // At this point, you should let the user know that he already has an account
                // but with a different provider, and let him validate the fact he wants to
                // sign in with this provider.
                // Sign in to provider. Note: browsers usually block popup triggered asynchronously,
                // so in real scenario you should ask the user to click on a "continue" button
                // that will trigger the signInWithPopup.
                firebase.auth().signInWithPopup(provider).then( (result) => {
                    // Remember that the user may have signed in with an account that has a different email
                    // address than the first one. This can happen as Firebase doesn't control the provider's
                    // sign in flow and the user is free to login using whichever account he owns.
                    // Step 4b.
                    // Link to Google credential.
                    // As we have access to the pending credential, we can directly call the link method.
                    const resultingUser = result.user;
                    result.user.link(pendingCred).then( (user) => {
                    // Google account successfully linked to the existing Firebase user.
                    this.authState = user; // goToApp();
                    }).catch( (errorInLinking) => {
                        console.log(errorInLinking);
                    });
                });
            });
        }
    });
}

如果我遗漏了什么,请告诉我。 谢谢!

最佳答案

更改为result.user.linkWithCredential(pendingCred).then( (user) ...4.0.0 版本开始,link 已被弃用,取而代之的是 linkWithCredential

关于javascript - result.user.link 不是 Angular Firebase 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47543403/

相关文章:

javascript+jquery Monkey修补/替换/覆盖类方法

javascript - 新的 Electron 浏览器窗口从指定的 ui-router 状态开始

jquery - 使用组件中的 id 定位一个元素

javascript - Json Pipe - 无法将字段设置为空

javascript - 如何在 Handlebars 中使用 json 和 nodeJs expressJs

javascript - 如何解析 JSON 对象并将值作为字符串显示在屏幕上?

angular - @ngrx 出现“NullInjectorError : No provider for t!”错误

node.js - 如何使用 Dialogflow 执行 Firebase 身份验证

Firebase:实时观察电子邮件验证状态

typescript - 无法使用 Ngrx 读取未定义的属性 'firebaseApp'