【问题标题】:Property 'accessToken' does not exist on type 'AuthCredential'“AuthCredential”类型上不存在属性“accessToken”
【发布时间】:2019-04-26 18:29:40
【问题描述】:

通过运行 ionic serve 我得到以下错误:

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 属性?

【问题讨论】:

  • 你解决了这个问题吗?
  • @CamiloCasadiego 将 let token = result.credential.accessToken; 替换为 let token = (&lt;any&gt;result).credential.accessToken;

标签: typescript firebase firebase-authentication angularfire2


【解决方案1】:

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

【讨论】:

  • 你有导入位置吗,我找不到从@angular/fire导入OAuthCredential的地方,自动导入也找不到。
  • 这行得通,但我们怎么知道这是一个好习惯呢?如果 Firebase 希望我们能够从身份验证结果中获取 accessToken(或在我的情况下为 idToken),他们会在回调中公开它,你不觉得吗?
【解决方案2】:

正如@bojeil 所建议的,铸造对我有用

firebase
      .auth()
      .signInWithPopup(this.provider)
      .then(result => {
        // This gives you a Google Access Token. You can use it to access the Google API.
        const credential = result.credential as firebase.auth.OAuthCredential;
        const token = credential.accessToken;
        // The signed-in user info.
        const user = result.user;

【讨论】:

    【解决方案3】:

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

    替换

    let token = result.credential.accessToken;

    let token = (&lt;any&gt;result).credential.accessToken;

    【讨论】:

    • 您好,我如何在 Objective-C 中做到这一点?
    【解决方案4】:

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

    【讨论】:

    • 那是android文档,没有javascript文档。
    猜你喜欢
    • 2022-11-07
    • 2021-01-09
    • 2021-07-10
    • 1970-01-01
    • 2017-07-22
    • 2023-03-26
    • 2023-04-02
    • 2019-12-20
    相关资源
    最近更新 更多