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

Using auth0/angular2-jwt with lazy loaded modules does not work as expected

Open marcojahn opened this issue 9 years ago • 10 comments

Affected Versions: Angular 4.4.4 auth0/angular2-jwt: 1.0.0-beta.9

When using JWT implementation with lazy loaded modules the interceptor is not applied to services using HttpClient provides in submodules.

As we have gathered so far is that this behaviour is working as expected as every module has its own dependency tree.

Switching from lazy loading to eager loading does not work to (requesting the module using a lambda expression with loadChildren).

As far as we can see there are two options that are working

  1. move all services (even these from the feature modules) to the core/application module
  2. provide a forChild option as angular did with its router for your JWT implementation.

As we can use option 1 for now it would be nice to have a implementation for reusing the JWT provider in sub modules that we can import/provide JWT in all modules

marcojahn avatar Oct 13 '17 09:10 marcojahn

We found a second workaround (reduced to a minimum amount of code).

// AppComponent
export let AppHttpClient: HttpClient;

@Component()
export class AppComponent implements OnInit {
  constructor(
    private httpClient: HttpClient
  ) {
    AppHttpClient = this.httpClient;
  }

// every service within a lazy loaded module
@Injectable()
export class FooService {
  httpClient: HttpClient;

  constructor( ) {
    this.httpClient = AppHttpClient;

marcojahn avatar Oct 13 '17 09:10 marcojahn

thanks @marcojahn you saved me, I was with the same problem

arthurfrancioni avatar Oct 13 '17 14:10 arthurfrancioni

But using the "global" AppHttpClient is crashing our tests (HttpClient is not injected anymore). This is why we are going to use the first approach (provide all services within app.module.ts) for now.

marcojahn avatar Oct 13 '17 14:10 marcojahn

Thanks for letting me know about this @marcojahn, let's wait for the implementation in lazy loaded modules

arthurfrancioni avatar Oct 13 '17 14:10 arthurfrancioni

@marcojahn I am having problems with autocomplete, it is not adding the token in the submodule requests, did you go through this?

arthurfrancioni avatar Oct 16 '17 10:10 arthurfrancioni

Using one of the workarounds should fix the missing token.

We have switched to the First one for not (provide all services at application module level) for now

marcojahn avatar Oct 16 '17 14:10 marcojahn

There should be JwtModule.forFeature method for lazy loaded feature modules. Can we expect implementation soon?

JacekKosciesza avatar Oct 21 '17 15:10 JacekKosciesza

A workaround that we are using is to duplicate what .forRoot() does in our lazy loaded modules:

import { HTTP_INTERCEPTORS } from ‘@angular/common/http’; import { JwtInterceptor } from ‘@auth0/angular-jwt’; ... providers: [ { provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true }, ... etc. ]

Duplicating the providers seems to be sufficient to enable the header token.

mbystedt avatar Nov 01 '17 18:11 mbystedt

Thanks @marcojahn! +1 for forChild implementation.

mfodor avatar Jan 29 '18 13:01 mfodor

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 04:10 stale[bot]