Skip to content

Can't use promise within tokenGetter in  #126

@ethanmay

Description

@ethanmay

I am using Ionic 2 with a custom Store provider to get/set token data using the native SecureStorage library with a LocalStorage fallback. However, when I attempt to use said store provider to get the token, then return the token from the promise, it appears that the tokenGetter does not wait for the promise to be completed before attempting to process the token.

ionicBootstrap( MyFancyApp, [
  Store,
  HTTP_PROVIDERS,
  AUTH_PROVIDERS,
  provide( AuthHttp, {
    useFactory: ( http, store ) => {
      return new AuthHttp( new AuthConfig({
        headerName: 'Authorization',
        headerPrefix: 'Bearer',
        tokenName: '',
        tokenGetter: ( () => {
          store.get('bearer').then( token => {
            console.log( token );
            return token;
          });
        }),
        globalHeaders: [{'Content-Type':'application/json'}]
      }), http );
    },
    deps: [ Http, Store ]
  })
]);

When I attempt to use AuthHttp to make a request, I get the following feedback in the console:
screen shot 2016-08-11 at 3 53 41 pm
It appears that the error is thrown by AuthHttp before the token is logged to the console, which is what leads me to think it isn't waiting for the promise. Any ideas? I'm pretty new to Angular 2 so I could just be doing this wrong. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions