Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2-jwt : useFactory directive being ignored

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

like image 245
prime Avatar asked Aug 01 '26 04:08

prime


1 Answers

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'}],
      })      
  ],
like image 54
prime Avatar answered Aug 02 '26 18:08

prime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!