angular2-jwt icon indicating copy to clipboard operation
angular2-jwt copied to clipboard

Incompatability with Ionic2 Storage (outdated access to storage)

Open moatazsoli opened this issue 9 years ago • 12 comments

The angular2-jwt page has some instruction for configuring jwt with ionic2

The instructions are outdated and doesn't work anymore. Ionic2 has updated the storage module to be used as injectable , please see https://ionicframework.com/docs/v2/storage/

import { Http } from '@angular/http';
import { Storage } from '@ionic/storage';

let storage = new Storage();

export function getAuthHttp(http) {
  return new AuthHttp(new AuthConfig({
    headerPrefix: YOUR_HEADER_PREFIX,
    noJwtError: true,
    globalHeaders: [{'Accept': 'application/json'}],
    tokenGetter: (() => storage.get('id_token')),
  }), http);
}

let storage = new Storage(); Doesn't work anymore.

Could you please update this to use the new storage injectable in ionic storage page

moatazsoli avatar Mar 30 '17 16:03 moatazsoli

The "fix" is actually quite simple. Just use storage as second parameter of the function (and remove the let storage = new Storage() assignment)

export function getAuthHttp(http, storage) {
    return new AuthHttp(new AuthConfig({
        headerPrefix: '',
        noJwtError: true,
        globalHeaders: [{'Accept': 'application/json', 'Content-Type': 'application/json'}],
        tokenGetter: (() => storage.get('id_token')),
    }), http);
}

and then at the providers just add storage as dependency

providers: [
        {
            provide: AuthHttp,
            useFactory: getAuthHttp,
            deps: [Http, Storage]
        }

ngehlert avatar Apr 01 '17 23:04 ngehlert

I get Cannot read property 'get' of undefined , any idea?

dearibujar avatar Apr 20 '17 12:04 dearibujar

I think storage.get is a promise so you may have to do this instead:

storage.get('id_token').then((val)=>{return val});

I havent tested this yet but will be trying to implement within the next day or so will let you know how I go,

bkarv avatar Apr 29 '17 04:04 bkarv

angular2-jwt supports resolving promises, specifically to support ionic storage. You don't need a .then. If get is undefined then you haven't setup storage properly.

escardin avatar May 01 '17 12:05 escardin

Can't resolve all parameters for Storage: (?).

enraiser avatar May 18 '17 12:05 enraiser

@enraiser

Can't resolve all parameters for Storage: (?).

this is basically the issue...

@ngehlert already posted a solution for this:

import { Storage } from '@ionic/storage';
...
export function getAuthHttp(http, storage) {
    return new AuthHttp(new AuthConfig({
        headerPrefix: '',
        noJwtError: true,
        globalHeaders: [{'Accept': 'application/json', 'Content-Type': 'application/json'}],
        tokenGetter: (() => storage.get('id_token')),
    }), http);
}
...
@NgModule({
    ...
    providers: [
        {
            provide: AuthHttp,
            useFactory: getAuthHttp,
            deps: [Http, Storage]
        }
    ]
})

Note that the parameters of the getAuthHttp function changed and therefore also the dependencies in the AuthHttp provider (Storage was added there too).

maybe the docs could give you a better understanding of the storage engine.

hansvn avatar Jun 14 '17 14:06 hansvn

Was there a fix for this?

Can't resolve all parameters for Storage: (?).

karlpatrickespiritu avatar Jul 21 '17 01:07 karlpatrickespiritu

I'm also getting the Can't resolve all parameters for Storage: (?). error. Tried suggestion above and doesn't seem to fix the issue. Anyone solved this kind of error?

karlpatrickespiritu avatar Jul 21 '17 03:07 karlpatrickespiritu

I had the same issue with the latest versions. I use an older version and it works (unfortunately I am unable to tell with version from the files I have but I am guessing 0.1.7 or 0.1.4 will work).

After that all I do is the following to use it in my service:

import { JwtHelper } from 'angular2-jwt'; In export class jwtHelper: JwtHelper = new JwtHelper();

bkarv avatar Jul 21 '17 04:07 bkarv

let storage = new Storage({});

export function getAuthHttp(http: any) {
  return new AuthHttp(new AuthConfig({
    headerPrefix: "Bearer",
    noJwtError: true,
    globalHeaders: [{'Accept': 'application/json'}],
    tokenGetter: (() => storage.get('token')),
  }), http);
}

In my app.module work for me.

lypborges avatar Jul 23 '17 14:07 lypborges

Just solved my issue of Can't resolve all parameters for Storage: (?)..

After hours of searching, I found out that I had a component that had Storage as a provider. So what I did was removed it.

@Component({
  selector: 'login',
  templateUrl: 'login.component.html',
  providers: [
    AuthService,
    // Storage, <-- removed this one
    JwtHelper
  ]
})

karlpatrickespiritu avatar Jul 26 '17 03:07 karlpatrickespiritu

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! 🙇‍♂️

stale[bot] avatar Oct 26 '19 05:10 stale[bot]