Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'accessToken' does not exist on type 'AuthCredential'

By running ionic serve I get the following error:

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

I have updated my firebase from firebase: ^4.12.1 to firebase: ^5.5.9

This is my ionic info output

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 ([email protected])
   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

and this is the method in the 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);
      });
   });
  }
}

Why does the accessToken property not exist on the AuthCredential object?

like image 918
Sireini Avatar asked Nov 24 '18 13:11

Sireini


3 Answers

As suggested by @bojeil casting worked for me

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;
like image 155
Jonathan Avatar answered Oct 30 '22 22:10

Jonathan


You need to cast the AuthCredential to OAuthCredential which has the accessToken property: https://github.com/firebase/firebase-js-sdk/blob/master/packages/auth-types/index.d.ts#L244

like image 21
bojeil Avatar answered Oct 31 '22 00:10

bojeil


The error occurs due to typescript definitions

Replace

let token = result.credential.accessToken;

with

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

like image 21
dxt Avatar answered Oct 31 '22 00:10

dxt