IdentityManager problems

363
1
02-06-2021 11:17 AM
JJames
by
New Contributor

I've been creating a web app utilizing Esri React Boot (which is a boilerplate that uses React, Redux plus Javascript). The app utilizes ArcGIS REST JS to login a user using a UserSession , then stores the login info from UserSession to a Redux store. 

The problem I'm encountering, is that when I try to register this UserSession to a Javascript 4.18 IdentityManager (using UserSession.toCredentials() and IdentityManager.registerToken()) after the user has logged in, I get an error: "Uncaught SyntaxError: Unexpected token in JSON at position 0." The goal is that once the user is logged in once, they don't have to login again to access portal items but can use the existing login credentials to access any private items.

After doing a little digging, it looks like this unexpected token is the Client ID I'm using in UserSession.completeOAuth2() and is getting tripped up on this part of the code: 

 

  _getOAuthHash() {
    let e = window.location.hash;
    if (e) {
      '#' === e.charAt(0) && (e = e.substring(1));
      const t = u(e);
      let r = !1;
      t.access_token && t.expires_in && t.state && t.hasOwnProperty('username')
        ? ((t.state = JSON.parse(t.state)), (this._oAuthHash = t), (r = !0))
        : t.error &&
          t.error_description &&
          (console.log('IdentityManager OAuth Error: ', t.error, ' - ', t.error_description),
          'access_denied' === t.error && (r = !0)),
        r && (window.location.hash = ('object' == typeof t.state && t.state.hash) || '');
    }
  }

 

where for some reason t.state is just returning the clientID string and not a JSON to be parsed.

Any ideas what's going wrong? This happens whenever I use IdentityManager (even if I'm just doing a console.log to see if it's already initialized)

Here's the code I'm using to register with IdentityManager:

 

function completeSignIn(options) {
  const {
    portalUrl = 'https://www.arcgis.com/',
    clientId,
    sessionId = `${portalUrl}_session`
  } = options;

  const session = UserSession.completeOAuth2({ clientId, portalUrl });
  const credentials = session.toCredential();
  IdentityManager.registerToken(credentials);

  const token = session.token;

  saveSession(session, sessionId);

  const user = await session.getUser();

  const portal = await getPortal(null, {
    portal: session.portal,
    authentication: session
  });

  return { user, portal, token };
}

 

0 Kudos
1 Reply
JeffK
by MVP Regular Contributor
MVP Regular Contributor

What do you get if you do the stringify / parse combo on your t.state?

 

JSON.parse(JSON.stringify(t.state))),

 

Or wrap it in another JSON.parse()?

0 Kudos