IdentityModel / oidc-client-js

OpenID Connect (OIDC) and OAuth2 protocol support for browser-based JavaScript applications
Apache License 2.0
2.43k stars 840 forks source link

addUserLoaded only triggered when token is renewed #1180

Closed shesellsanctuary closed 3 years ago

shesellsanctuary commented 4 years ago

Hello, I have the following code in attempt to implement the Authorization flow with redux after making it work without redux, so the flow happens ok every time, but the UserLoaded event is not triggered when the signinRedirect happens. I have seen in the UserManagerEvents.js that it should be raised after a successfull login, so I don't know why it is not being raised in my code. Do you have any ideas?

const userManager = new UserManager(settings);

  userManager.events.addUserLoaded((user) => {
    dispatch(userLoaded(user.profile.name));
  });
  userManager.events.addUserUnloaded(() => {
    dispatch(userUnloaded());
  });

(This is my first git issue ever, please let me know if you need more info)

brockallen commented 4 years ago

If your login callback handling code is on a different instance of the UserManager (because it's running on a diff page) then perhaps that's why?

shesellsanctuary commented 4 years ago

I think that is it. We have a html callback page that uses another usermanager instance, because based on this OAuth guidelines there cannot be fragments in the redirect url. So there is no way of using the event this way?

brockallen commented 4 years ago

Right, so then on that callback page redirect back into your main app. The main app usually would call getUser() on load to know about the user's session.

shesellsanctuary commented 4 years ago

Right, thank you, but the getUser() method does not raise the user loaded event.

   getUser() {
        return this._loadUser().then(user => {
            if (user) {
                Log.info("UserManager.getUser: user loaded");

                this._events.load(user, false);

                return user;
            }
            else {
                Log.info("UserManager.getUser: user not found in storage");
                return null;
            }
        });
    }

Anyway we can create another method that calls the getUser() and dispatches our action then. Thank you for the insight!

brockallen commented 3 years ago

I think you're all set on this now so closing.