title: getIdToken from firebase.User in typescript
body: <pre><code>import { AngularFireAuth } from 'angularfire2/auth';
  
  import * as firebase from 'firebase/app';
  
  @Injectable()
  export class GetRequestsProvider {
  
    user: firebase.User;
    userIdToken:string;
  
    constructor(public http: HttpClient, public afAuth: AngularFireAuth, authservices: AuthService) {
        afAuth.authState.subscribe(user =&gt; {
          this.user = user;
        });
    }
  
    getUserToken(){
  
        this.afAuth.auth.onAuthStateChanged((user) =&gt; {
  
            user.getIdToken().then((idToken)=&gt;{
  
              this.userIdToken = idToken;
              console.log("Here I get the user token correctly: ", this.userIdToken);
              console.log(typeof(this.userIdToken)); // ‘userIdToken’ type is ‘string’
  
            });
  
            console.log("Here the variable ‘userIdToken’ is undefined: ", this.userIdToken);
        }     
     }    
  }
  </code></pre>
  
  <p>I'm trying to use the <a href="https://firebase.google.com/docs/reference/js/firebase.User#getIdToken" rel="nofollow noreferrer">getIdToken</a> method from Firebase to authenticate users in my app.</p>
  
  <p>The problem I have now, after successfully getting the user’s token, I am not able to save it into a variable or to use the token somewhere else.</p>