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
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;
thanks @marcojahn you saved me, I was with the same problem
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.
Thanks for letting me know about this @marcojahn, let's wait for the implementation in lazy loaded modules
@marcojahn I am having problems with autocomplete, it is not adding the token in the submodule requests, did you go through this?
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
There should be JwtModule.forFeature method for lazy loaded feature modules. Can we expect implementation soon?
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.
Thanks @marcojahn!
+1 for forChild implementation.
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! 🙇♂️
Most helpful comment
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.