Configuring Angular2-JWT in Ionic 3 - Type Error


#1

Hi everyone I am trying to configure my app.module to include Angular2-JWT. I’ve followed Angular2-jwt readme as well converting my storage.get to a promise but I get the following error:

TypeError: StorageConstructor is not a constructor (evaluating 'new Storage()')

Can anyone help me with setting this up properly? Thanks in advance.
Note I’ve also tried import storage but then get parameter error.

My code looks like this.

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { Http, HttpModule } from '@angular/http';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { IonicStorageModule } from '@ionic/storage';
import { AuthHttp, AuthConfig } from 'angular2-jwt';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
import { ProfilePage } from '../pages/profile/profile';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { AuthService } from '../providers/auth/auth.service';
import { AuthResourceService } from '../providers/auth/auth-resource.service';
import { AlertService } from '../providers/alert/alert.service';

let storage = new Storage();

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

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    LoginPage,
    ProfilePage
  ],
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp),
    IonicStorageModule.forRoot()
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    LoginPage,
    ProfilePage
  ],
  providers: [
    {
      provide: AuthHttp,
      useFactory: getAuthHttp,
      deps: [Http]
    },    
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    AuthService,
    AuthResourceService,
    AlertService
  ]
})
export class AppModule {}

Best Practice for Storing User Session
#2

See angular2-jwt #323.


#3

Awesome resolved thanks!