# Angular 11 Lazy loaded module, missing token in service request

**URL:** <https://community.auth0.com/t/angular-11-lazy-loaded-module-missing-token-in-service-request/61772>\
**Category:** Get Help\
**Tags:** jwt, auth0\
**Created:** [April 26, 2021, 7:47pm UTC](https://community.auth0.com/t/angular-11-lazy-loaded-module-missing-token-in-service-request/61772 "2021-04-26T19:47:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgoudman](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pgoudman/32/13037_2.png) [@pgoudman](https://community.auth0.com/u/pgoudman)\
**Post date:** [April 26, 2021, 7:47pm UTC](https://community.auth0.com/t/angular-11-lazy-loaded-module-missing-token-in-service-request/61772/1 "2021-04-26T19:47:20Z")

</div>

Hi,

I have an Angular 11 SPA with a lazy loaded module.

I am using “@auth0/auth0-angular”: “^1.3.2”

The lazy loaded module is protected by AuthGuard from “@auth0/auth0-angular”;

I have also added this to my lazy loaded module file to try and get the AuthGuard to add the token.  
{provide: HTTP\_INTERCEPTORS, useClass: AuthHttpInterceptor, multi: true}

But the token is missing in the lazy loaded module’s service request?

Do you have any suggestions as to a solution.

Thank you  
Peter

---

<div class="post-metadata">

**Author:** ![pgoudman](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pgoudman/32/13037_2.png) [@pgoudman](https://community.auth0.com/u/pgoudman)\
**Post date:** [April 29, 2021, 3:18pm UTC](https://community.auth0.com/t/angular-11-lazy-loaded-module-missing-token-in-service-request/61772/2 "2021-04-29T15:18:32Z")

</div>

Waited a few days hoping for an answer or suggestion but sadly no. So here is what I have done for an Angular 11 Lazy Loaded module.

Created a HttpInterceptor for my lazy loaded module that adds the token to the request.

Here is the code if anyone has a similar issue and needs a workaround.

```
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpEvent, HttpResponse, HttpRequest, HttpHandler } from '@angular/common/http';
import { Observable } from 'rxjs';
import {AuthService} from "@auth0/auth0-angular";
import {mergeMap} from "rxjs/operators";

@Injectable()
export class AppHttpInterceptor implements HttpInterceptor {

  constructor(private auth: AuthService) {
  }
  intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return this.auth.getAccessTokenSilently().pipe(
      mergeMap(token => {
      // Add token to request
        const tokenReq = httpRequest.clone({
        setHeaders:{ Authorization: `Bearer ${token}`}
      });
      return next.handle(tokenReq);
    }));
  }
}

```

In my lazy loaded module I needed to have these declarations for the token to be added.

```
providers: [
  // Auth0 interceptor that is not getting called.
  // Interestingly if you remove this interceptor the token never gets fetched.
  {provide: HTTP_INTERCEPTORS, useClass: AppHttpInterceptor, multi: true},
  // My custom interceptor to add the token.
  {provide: HTTP_INTERCEPTORS, useClass: AuthHttpInterceptor, multi: true},

```

]

---

<div class="post-metadata">

**Author:** ![mohammadz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mohammadz/32/13482_2.png) [@mohammadz](https://community.auth0.com/u/mohammadz)\
**Post date:** [April 29, 2021, 10:32pm UTC](https://community.auth0.com/t/angular-11-lazy-loaded-module-missing-token-in-service-request/61772/3 "2021-04-29T22:32:57Z")

</div>

I had the same problem for my angular 10 app. I was also able accomplish this the same way using getAccessTokenSilently() for my requests.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 30, 2021, 8:47am UTC](https://community.auth0.com/t/angular-11-lazy-loaded-module-missing-token-in-service-request/61772/4 "2021-04-30T08:47:43Z")

</div>

Thanks for sharing it with the rest of community!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [May 15, 2021, 8:47am UTC](https://community.auth0.com/t/angular-11-lazy-loaded-module-missing-token-in-service-request/61772/5 "2021-05-15T08:47:47Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
