Giter Site home page Giter Site logo

Comments (8)

luisrudge avatar luisrudge commented on August 15, 2024 2

Thanks for posting this @Schmaga! We recently upgraded our Angular Quickstart with the shareReplaymethod as well. Here's our take on a great AuthService: https://auth0.com/docs/quickstart/spa/angular2/01-login#add-an-authentication-service

from auth0-spa-js.

Schmaga avatar Schmaga commented on August 15, 2024 1

This is a duplicate of my post from the auth0 community forum, but I hope it might help someome:

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.

from auth0-spa-js.

Tazaf avatar Tazaf commented on August 15, 2024 1

@luisrudge I've seen that! And I got to say that this is a waaaay better AuthService than the one before 👍
(By the way, I did not run onto this thread issue)

from auth0-spa-js.

luisrudge avatar luisrudge commented on August 15, 2024

This is probably happening because the iframe was already closed 🤔 Can you inspect dev tools and see if you're sending two requests to the /authorize endpoint?

from auth0-spa-js.

gosuhiman avatar gosuhiman commented on August 15, 2024

I've got the same problem, and there are 2 requests sent to /authorize.

from auth0-spa-js.

rkuprin avatar rkuprin commented on August 15, 2024

Same problem, two requests. Happens only if you refresh a page after sign-in (I'm using Auth0 with Angular SPA), without refreshing it works fine and sends just one request.

from auth0-spa-js.

luisrudge avatar luisrudge commented on August 15, 2024

@gosuhiman @rkuprin Is it possible that you have two instances of the client? can you build a repro to help me identify why are you seeing two requests?

from auth0-spa-js.

max-eb avatar max-eb commented on August 15, 2024

Same problem if you write code like this:

class Auth { 
 getAuthClient = () => new Promise((resolve) => {
    if (this.auth0Client) {
      return resolve(this.auth0Client);
    }

    return resolve(createAuth0Client({
    domain: DOMAIN,
    client_id: CLIENT_ID,
    connection: DATABASE,
    audience: AUDIENCE,
    redirect_uri: ORIGIN,
    }).then(auth0Client => resolve(auth0Client)));
  });

  getUserInfo = async () => {
    const auth0Client = await this.getAuthClient();
    return auth0Client.getUser();
  }
}

If you call getUserInfo in several places in your project, after refreshing page, it will invoke function createAuth0Client several times.
In function createAuth0Client, it will call getTokenSilently. That is, if you invoke createAuth0Client multiple times, it will use a same iframe window(not sure). Once a tread called close, the other thread will fail.
So I changed the code to this:

class Auth {
  constructor() {
    this.setUp();
  }

  setUp = async () => createAuth0Client({
    domain: DOMAIN,
    client_id: CLIENT_ID,
    connection: DATABASE,
    audience: AUDIENCE,
    redirect_uri: ORIGIN,
  }).then((auth0Client) => {
    this.auth0Client = auth0Client;
  });

  getAuthClient = () => new Promise((resolve) => {
    const checkAuth0Ready = setInterval(() => {
      if (this.auth0Client) {
        resolve(this.auth0Client);
        clearInterval(checkAuth0Ready);
      }
    }, 500);

    setTimeout(() => clearInterval(checkAuth0Ready), 10000);
  });

  getUserInfo = async () => {
    const auth0Client = await this.getAuthClient();
    return auth0Client.getUser();
  }

And it doesn't throw exceptions now.

from auth0-spa-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.