3

I'm struggling to integrate the angular2-jwt package. I've written the following function with custom configuration options

import { Http, Headers, RequestOptions } from '@angular/http';
import { provideAuth, AuthHttp, AuthConfig } from 'angular2-jwt';
import { Injectable } from '@angular/core';

export function authHttpServiceFactory(http: Http, options: RequestOptions) {

    return new AuthHttp( new AuthConfig({
        noTokenScheme: true,
        tokenGetter: (() => JSON.parse(     localStorage.getItem('access_token') ) ),
        globalHeaders: [{'Accept': 'application/json'}],
    }), this.http, this.options); 
}

and inside my module definition I have included the lines

providers: [
    ...,
    {
      provide: AuthHttp,
      useFactory: authHttpServiceFactory,
      deps: [Http, RequestOptions]
    }        
],

but when I try to use AuthHttp inside a service it appears to ignore my factory method and returns a default implementation:

import { AuthHttp } from 'angular2-jwt';

@Injectable() export class SsoService {

constructor (
    private authHttp: AuthHttp,
) {}

Is my module configuration correct? Have I missed something simple

prime
  • 2,004
  • 3
  • 28
  • 45

1 Answers1

2

For anybody else I changed the module to use an inline solution instead of the factory class to read as follows:

  providers: [
      ...
      AuthHttp,
      provideAuth({
          noTokenScheme: true,
          tokenGetter: (() => JSON.parse( localStorage.getItem('access_token') ) ),
          globalHeaders: [{'Accept': 'application/json'}],
      })      
  ],
prime
  • 2,004
  • 3
  • 28
  • 45