<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Initialize SessionManager(ExB) from already signed in user in ArcGIS Experience Builder Questions</title>
    <link>https://community.esri.com/t5/arcgis-experience-builder-questions/initialize-sessionmanager-exb-from-already-signed/m-p/1651338#M20862</link>
    <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/2564"&gt;@RyanTaylor&lt;/a&gt;&amp;nbsp;You can use the extension for the "APP_CONFIG_PROCESSOR" extension point to make sure the logic is run before all other widgets, here is a sample to show how to use this extension:&amp;nbsp;&lt;A href="https://github.com/Esri/arcgis-experience-builder-sdk-resources/tree/master/widgets/translation" target="_blank"&gt;https://github.com/Esri/arcgis-experience-builder-sdk-resources/tree/master/widgets/translation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think the way you register the token should work, to refresh the token, you can use your own logic to refresh the token and then run the above logic again.&lt;/P&gt;</description>
    <pubDate>Thu, 18 Sep 2025 05:34:28 GMT</pubDate>
    <dc:creator>JunshanLiu</dc:creator>
    <dc:date>2025-09-18T05:34:28Z</dc:date>
    <item>
      <title>Initialize SessionManager(ExB) from already signed in user</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/initialize-sessionmanager-exb-from-already-signed/m-p/1605764#M18454</link>
      <description>&lt;P&gt;We've built a web application with Experience Builder (frontend) and ASP.NET Core (backend). ASP.NET Core both serves the frontend at&amp;nbsp;&lt;A href="https://example.com/exb" target="_blank" rel="noopener"&gt;https://example.com/exb&lt;/A&gt;&amp;nbsp;and provides custom REST API's at &lt;A href="https://example.com/exb/api" target="_blank" rel="noopener"&gt;https://example.com/exb/api&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;No content can be loaded unless the user has authenticated, including the JavaScript files that comprise the ExB app. When an unauthenticated user opens &lt;A href="https://example.com/exb" target="_blank" rel="noopener"&gt;https://example.com/exb&lt;/A&gt; ASP.NET begins an a OAuth flow against ArcGIS Portal. Once signed in, ASP.NET returns a cookie that is required for all future requests to &lt;A href="https://example.com/exb" target="_blank" rel="noopener"&gt;https://example.com/exb&lt;/A&gt; and child routes.&lt;/P&gt;&lt;P&gt;The access and refresh tokens one obtains from the OAuth flow with portal are embedded in the cookie. I can access these via a custom API (e.g., exb/api/token) to return these to ExB as needed.&lt;/P&gt;&lt;P&gt;We'd like to initialize ExB's&amp;nbsp;SessionManager/ArcGISIdentityManager with our already signed in user as we need to prevent ExB from starting a secondary OAuth flow when accessing secured ArcGIS Enterprise services. We should be able to do this using the existing access &amp;amp; refresh token's but we're running into a bit of trouble. Here is our current attempt...&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// login widget
import { React, SessionManager, type AllWidgetProps } from "jimu-core";
import type { IMConfig } from "../config";
import { useSignInFromToken } from "../hooks/auth";

const Widget = (props: AllWidgetProps&amp;lt;IMConfig&amp;gt;) =&amp;gt; {
  const clientId = "id-registered-in-arcgis-portal";
  useSignInFromToken(props.portalUrl, clientId);

  const mainSession = SessionManager.getInstance().getMainSession();

  return (
    &amp;lt;div style={{ backgroundColor: "whitesmoke" }}&amp;gt;
      &amp;lt;p&amp;gt;Login&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Portal: {mainSession?.portal ?? "unknown"} &amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;User: {mainSession?.username ?? "unknown"}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Token: {mainSession?.token ?? "unknown"}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Widget;&lt;/LI-CODE&gt;&lt;LI-CODE lang="javascript"&gt;// auth.ts (hook)
import { ArcGISIdentityManager } from "@esri/arcgis-rest-request";
import { React, SessionManager } from "jimu-core";
import esriId from "@arcgis/core/identity/IdentityManager";

import { fetchToken } from "../api/token";
const { useEffect } = React;

export type AccessToken = string;

export function useSignInFromToken(portalUrl: string, clientId: string) {
  useEffect(() =&amp;gt; {
    async function addOrReplaceSessionFromToken() {
      try {
        const accessTokenExpiration = new Date(Date.now());
        accessTokenExpiration.setMinutes(accessTokenExpiration.getMinutes() + 30);

        const userInfo = await fetchToken();
        const idManager = await ArcGISIdentityManager.fromToken({
          token: userInfo.accessToken,
          clientId: clientId,
          portal: `${portalUrl}/sharing/rest`,
          username: userInfo.userName,
          tokenExpires: accessTokenExpiration
          // todo: fill these too?
          // redirectUri: "",
          // server: "",
        });

        const cred = idManager.toCredential();
        esriId.registerToken(cred);

        SessionManager.getInstance().addOrReplaceSession(idManager);
        // was thinking this 👇 would create the JSAPITOAuth in Session Storage
        // but it does not.
        SessionManager.getInstance().initSession();
      } catch (err) {
        console.error("error", err);
      }
    }

    addOrReplaceSessionFromToken();
  }, [portalUrl, clientId]);
}&lt;/LI-CODE&gt;&lt;P&gt;I've added the Widget to my experience as an always rendered widget. It's near the top of the Widget "stack", though I'm not sure that matters.&amp;nbsp;&lt;/P&gt;&lt;P&gt;When this widget renders I fetch our access token, initialize a new ArcGISIdentityManager and add it to the SessionManager. However, sometimes ExB will still start it's own OAuth flow and other times it will not. This feels like a race condition. It seems that sometimes my widget doesn't finish setting the session before the Map and other widgets make their requests to Portal.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;A Few Questions&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Is there a way to force my code to run before all other widgets are rendered, either by setting up widget dependencies, or moving this code to another location?&lt;/P&gt;&lt;P&gt;Is the code above the correct way to initialize a session from an existing token? Do I need to include other parameters?&lt;/P&gt;&lt;P&gt;Why is the exb_auth local session variable created but not the JSAPIOAuth session variable? Do I need both for a fully hydrated session?&lt;/P&gt;&lt;P&gt;How can I properly handle access token refreshes? If the access token times out I need to get a new one. So long as the refresh token is valid I shouldn't have to have the user log in again. Can I repoint SessionManager/ArcGISIdentityManager to my own endpoint for getting a new access token?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jul 2025 18:47:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/initialize-sessionmanager-exb-from-already-signed/m-p/1605764#M18454</guid>
      <dc:creator>RyanTaylor</dc:creator>
      <dc:date>2025-07-31T18:47:03Z</dc:date>
    </item>
    <item>
      <title>Re: Initialize SessionManager(ExB) from already signed in user</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/initialize-sessionmanager-exb-from-already-signed/m-p/1651338#M20862</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/2564"&gt;@RyanTaylor&lt;/a&gt;&amp;nbsp;You can use the extension for the "APP_CONFIG_PROCESSOR" extension point to make sure the logic is run before all other widgets, here is a sample to show how to use this extension:&amp;nbsp;&lt;A href="https://github.com/Esri/arcgis-experience-builder-sdk-resources/tree/master/widgets/translation" target="_blank"&gt;https://github.com/Esri/arcgis-experience-builder-sdk-resources/tree/master/widgets/translation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think the way you register the token should work, to refresh the token, you can use your own logic to refresh the token and then run the above logic again.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Sep 2025 05:34:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/initialize-sessionmanager-exb-from-already-signed/m-p/1651338#M20862</guid>
      <dc:creator>JunshanLiu</dc:creator>
      <dc:date>2025-09-18T05:34:28Z</dc:date>
    </item>
  </channel>
</rss>

