添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
会开车的红金鱼  ·  Android ...·  3 月前    · 
买醉的拖把  ·  克诺索斯宫-抖音百科·  4 月前    · 

Hi everyone,

I am currently working on replacing the old auth0-js implementation of our application with your new auth0-spa-js library. Even though I got it to work somehow, I am always getting the following error on the console which is caught by my error handler:

TypeError: Cannot read property 'close' of undefined
    at a (auth0-spa-js.production.js:1)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Object.onInvokeTask (core.js:17290)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:498)
    at invokeTask (zone.js:1744)
    at globalZoneAwareCallback (zone.js:1781)

Here is the relevant code from the interceptor:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const isAuthenticated$ = from(this.authService.isAuthenticated());
    return isAuthenticated$.pipe(switchMap(isAuthenticated => {
      if (isAuthenticated) {
        const accessToken$ = from(this.authService.getAccessToken());
        return accessToken$.pipe(switchMap(token => {
          const headers = req.headers.set('Authorization', 'Bearer ' + token);
          const authReq = req.clone({ headers });
          return next.handle(authReq);
      } else {
        return next.handle(req);

And the auth-service:
private auth0Client: Auth0Client;

  config = {
    domain: 'xxx',
    client_id: 'yyy',
    redirect_uri: '...',
    audience: 'myaudience',
    responseType: 'token id_token',
    scope: 'openid email'
  private async createClientIfNecessary() {
    if (!this.auth0Client) {
      this.auth0Client = await createAuth0Client(this.config);
  async isAuthenticated() {
    await this.createClientIfNecessary();
    return await this.auth0Client.isAuthenticated();
  async login() {
    await this.createClientIfNecessary();
    await this.auth0Client.loginWithRedirect(this.config);
  async getAccessToken(): Promise<string> {
    await this.createClientIfNecessary();
    const token = await this.auth0Client.getTokenSilently();
    return token;

Anyone know what I am doing wrong? It is a lot of transforming of Promises to Observables, maybe that is the cause of the error. And it is actually a bit hacky to always check if the client is already created, but I wanted to encapsulate as much auth0 code as possible into the service.

Update: The error also occurs if I consequently wrap everything into Observables, even in the service itself.

The problem seems also to appear only under certain conditions, because If I attach the debugger and step through parts of a page load, the error does NOT occur…

Sadly, no. I cannot figure out whether I am doing anything wrong or if there is a problem in the library. I am hoping that someone from Auth0 Support has a look at this thread. Otherwise I would probably have to open a github issue. But it’s actually comforting to know that I am not the only one with that problem :wink:

Hi @schmaga,
I suggest to open a github issue at Issues · auth0/auth0-spa-js · GitHub so that the SDK team can take a look at it.

And can you exclude this as a possible reason?

I'm having an error in the console when creating the Auth0 client using `

I think I might have found the problem. Because of the observables and multiple subscriptions, there might have been concurrency issues or multiple instances of the Auth0Client. I got rid of the error by doing it this way:

Create the client as shared observable in a similar way like you proposed in your updated angular quickstart:

  private auth0Client$ = (from(createAuth0Client(this.config))).pipe(
    shareReplay(1), // Every subscription receives the same shared value

And using it using a piped observable.

isAuthenticated() {
 return this.auth0Client$.pipe(switchMap(client => {
 return from(client.isAuthenticated());

This has solved the error for me.