类型“ AuthCredential”上不存在属性“ accessToken”

时间:2018-11-24 13:27:57

标签: typescript firebase firebase-authentication angularfire2

通过运行离子服务,我会遇到以下错误:

Property 'accessToken' does not exist on type 'AuthCredential'.
// You can use it to access the Google API.
let token = result.credential.accessToken;

我已将我的Firebase从firebase: ^4.12.1更新为firebase: ^5.5.9

这是我的离子信息输出

Ionic:

   ionic (Ionic CLI)  : 4.1.2
   Ionic Framework    : ionic-angular 3.9.2
   @ionic/app-scripts : 3.1.9

Cordova:

   cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
   Cordova Platforms     : android 7.0.0
   Cordova Plugins       : no whitelisted plugins (19 plugins total)

System:

   NodeJS : v10.13.0 (C:\Program Files\nodejs\node.exe)
   npm    : 5.8.0
   OS     : Windows 10

这是auth.service.ts

中的方法
signInWithGoogle() {
   console.log('Sign in with google');
   return this.oauthSignIn(new firebase.auth.GoogleAuthProvider());
}

private oauthSignIn(provider: AuthProvider) {
   if (!(<any>window).cordova) {
        return this.afAuth.auth.signInWithPopup(provider).then(() => {
            this.saveUserDetails('gmail');
        });
   } else {
       return this.afAuth.auth.signInWithRedirect(provider)
           .then(() => {
              return this.afAuth.auth.getRedirectResult().then(result => {
                 // This gives you a Google Access Token.
                // You can use it to access the Google API.
                let token = result.credential.accessToken;
               // The signed-in user info.
                let result_user = result.user;

                this.saveUserDetails('gmail');

                console.log(token, result_user);
      }).catch(function (error) {
         // Handle Errors here.
         alert(error.message);
      });
   });
  }
}

为什么AuthCredential对象上不存在accessToken属性?

4 个答案:

答案 0 :(得分:5)

您需要将AuthCredential强制转换为具有OAuthCredential属性的accessTokenhttps://github.com/firebase/firebase-js-sdk/blob/master/packages/auth-types/index.d.ts#L244

答案 1 :(得分:1)

您需要查看AuthCredential的定义。如果是this one,则它没有accessToken属性。

答案 2 :(得分:0)

由于打字稿定义而发生错误

替换

let token = result.credential.accessToken;

let token = (<any>result).credential.accessToken;

答案 3 :(得分:0)

@bojeil演员推荐,对我有用

Mono<List<Entity2>>