Angular2-jwt: Unable to use tokenGetter with @ionic/storage 2.0.0

Created on 9 Mar 2017  ·  6Comments  ·  Source: auth0/angular2-jwt

Ionic Storage moved their logic to his own module (IonicStorageModule) and now you can't import Storage module to instanciate it and use it in tokenGetter method.
Can somebody help me? :disappointed:

stale

Most helpful comment

See issue #323 that is the real fix.

import { Storage, IonicStorageModule } from '@ionic/storage';

export function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig({noJwtError: true}), http, options);
}

export function getAuthHttp(http, storage) {
  return new AuthHttp(new AuthConfig({
    headerPrefix: 'Bearer',
    noJwtError: true,
    globalHeaders: [{'Accept': 'application/json'}],
    tokenGetter: (() => storage.get('access_token')),
  }), http);
}
@NgModule({
  imports: [
    ...
    IonicStorageModule.forRoot(),
   ...
  providers: [
    ...
    {
      provide: AuthHttp,
      useFactory: getAuthHttp,
      deps: [Http, Storage]
    },
    ...

All 6 comments

Mine is working. If you give us your code we might be able to help.

@acessoftware I don't know what happend but suddenly started working :open_mouth:

I'm currently having this issue. Attempting to do

function AuthHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig({
        noJwtError: true,
        tokenGetter: (() => Storage.get('jwt_token')),
        globalHeaders: [{'Content-Type':'application/json'}],
    }), http, options);
}

Doesn't work, informing me that "Property 'get' does not exist on type 'typeof Storage'".

Fixed it using the following snippet in a new file (not in the module file)

export function AuthHttpServiceFactory(http: Http, options: RequestOptions) {
    let _storage = new Storage({});
  return new AuthHttp(new AuthConfig({
        noJwtError: true,
        tokenGetter: (() => _storage.get('jwt_token')),
        globalHeaders: [{'Content-Type':'application/json'}],
    }), http, options);
}

See issue #323 that is the real fix.

import { Storage, IonicStorageModule } from '@ionic/storage';

export function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig({noJwtError: true}), http, options);
}

export function getAuthHttp(http, storage) {
  return new AuthHttp(new AuthConfig({
    headerPrefix: 'Bearer',
    noJwtError: true,
    globalHeaders: [{'Accept': 'application/json'}],
    tokenGetter: (() => storage.get('access_token')),
  }), http);
}
@NgModule({
  imports: [
    ...
    IonicStorageModule.forRoot(),
   ...
  providers: [
    ...
    {
      provide: AuthHttp,
      useFactory: getAuthHttp,
      deps: [Http, Storage]
    },
    ...

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇‍♂️

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ciesielskico picture ciesielskico  ·  5Comments

tekkudoc picture tekkudoc  ·  5Comments

Tirka picture Tirka  ·  3Comments

leosvelperez picture leosvelperez  ·  5Comments

kolkov picture kolkov  ·  3Comments