openid / AppAuth-JS

JavaScript client SDK for communicating with OAuth 2.0 and OpenID Connect providers.
Apache License 2.0
975 stars 162 forks source link

performTokenRequest() method does not return a promise #201

Closed sakshiigarg closed 2 years ago

sakshiigarg commented 2 years ago

[REQUIRED] Describe expected behavior

The piece of code inside then() should be resolved immediately in a synchronous fashion since performTokenRequest() is returning a promise.

this.tokenHandler.performTokenRequest(configuration, tokenRequest)
       .then((oResponse) => {
      this.sessionStorage.setItem('refresh_token', oResponse.refreshToken);
      this.sessionStorage.setItem('access_token', oResponse.accessToken);
      this.sessionStorage.setItem('id_token', oResponse.idToken);
     })
      .catch(oError => {
       this.error = oError;
      });     

Describe the problem

The problem here is that performTokenRequest() is not returning a Promise, due to which the tokens are received and stored asynchronously by the time other components of my application start loading. I need this piece of code to run synchronously so on application launch itself I am able to trigger authentication and without receiving the tokens I am not able to load the application.

[REQUIRED] Actual Behavior

Here, the control comes out of the tokenRequest method while other pieces of code start to be executed.

[REQUIRED] Steps to reproduce the behavior

In my constructor I have this piece of code:

this.notifier.setAuthorizationListener((request, response, error) => {
      console.log('Authorization request complete ', request, response, error);
      if (response) {
...
this.makeTokenRequest(this.configuration, request, response);
...
}

Method call:

makeTokenRequest(configuration, request, response): Promise<Token> {
    return new Promise( (resolve, reject) => {
      ...
      const oResponse =  this.tokenHandler.performTokenRequest(configuration, tokenRequest);
       .then((oResponse) => {
      this.sessionStorage.setItem('refresh_token', oResponse.refreshToken);
      this.sessionStorage.setItem('access_token', oResponse.accessToken);
      this.sessionStorage.setItem('id_token', oResponse.idToken);
     })
      .catch(oError => {
       this.error = oError;
      });     
   }
}

[REQUIRED] Environment

tikurahul commented 2 years ago

It returns a Promise<TokenResponse>. I am confused when you say it does not.

The code inside the then(...) is not guaranteed to be resolved synchronously if we need to refresh the tokens.