Portal.user is null

02-26-2021 08:04 PM
Occasional Contributor

Has anyone ever try the JS API 4.18 oauth sample at 
(save to a local file and run it on localhost or a Dev server) and then find that after signed in (with an AGOL for Organization paid user account) you get a portal.user is null error (from the web console) so that the app breaks on trying to get portal.user.username? 

When that happens, if you try to load your own secure layer (additional code) it will get a permission error and will not load, even though the credential exists in the Session Storage.

Why would the Identity Manager grant the login but Portal loads with a portal.user null result?  What good is the credential returned by the checkSignedInStatus() if you still cannot access your own services?


0 Kudos
2 Replies
New Contributor III

The code works for me, but what was not clear to me from that sample was when to instantiate the Portal item.

Here is a simple function I wrote that works in my application. You have to initialize the Portal object after the login is successful, using the checkSignInStatus();

After the _portal.load() is successfully executed, then my _portal object is populated with all the Portal properties for the sign-in user, including the user object (_portal.user).

Hope this helps...

            var _portal = null,
                _oAuthInfo = null;


            registerApplication: function () {
                _oAuthInfo = new OAuthInfo({
                    appId: 'abc123xyz'

                arcgisIdentityManager.checkSignInStatus(_oAuthInfo.portalUrl + "/sharing")
                    .then((r) => {
                        _portal = new Portal();


0 Kudos
Occasional Contributor

The basic OAuth sample worked for me too.  Basically I want to use the sample method for users to switch between secure mode and public view. For example, without login in, an anonymous user sees a non-editable public view layer that is a view of a protected editable feature layer which can then be added and edited if user with proper permission signs in. 

The bug comes up when you build on the sample with more functionality. After signing in, the portal loads. I see a fully populated portal object, with all the right properties , except for portal.user is null. IdentityManager.checkSignedInStatus returns a credential in Session Storage. If you replace portal.user.username with credential.userId you can even do a PortalQuery and list your AGOL items like the sample does. The token is valid because I can string it behind a secure service URL and open the REST endpoint. But my AGOL secure layer that I want to add to a map (after signedInStatus checked and portal loads) fails to load. 

I did not want to "pre-load" the secure, editable layer by default and prompt every user to login immediately upon opening the web page. I did discover that if you set esriConfig.request.useIdentity=false at the start of require() it will produce this bug.  Obviously there are probably other conflicting configs or custom modules in my app that created this issue, and I am now forced to go through my entire source code to debug it.  I was just wondering if this is a situation that others might have encountered.


0 Kudos