<?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: Use the BeginOAuth2 without a popup in ArcGIS REST JS Questions</title>
    <link>https://community.esri.com/t5/arcgis-rest-js-questions/use-the-beginoauth2-without-a-popup/m-p/1041054#M4</link>
    <description>&lt;P&gt;Hi, yes you can do that. Simply move the arcgisRest.UserSession.completeOAuth2() call into the main page. The key is to detect if you're in the state where the access_token has been passed by the URL. One way to do it would be something like this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;  // Define a global session variable.
  let session = null;

  // ADDED: ---------------------------------
  const clientIdFromURL = new URLSearchParams(window.location.search).get('clientID');
  const parsedHash = new URLSearchParams(
    window.location.hash.substr(1) // skip the first char (#)
  );
  var accessTokenFromURL = parsedHash.get('access_token');

  // only call completeOAuth2 if we have the clientID and access token:
  if(clientIdFromURL &amp;amp;&amp;amp; accessTokenFromURL) {
    session = arcgisRest.UserSession.completeOAuth2({
      clientId: clientIdFromURL,
    });
    localStorage.setItem('__ARCGIS_REST_USER_SESSION__', session.serialize());
  }
  // END ADDED SECTION: ---------------------------------

  // Check to see if there is a serialized session in local storage.
  const serializedSession = localStorage.getItem('__ARCGIS_REST_USER_SESSION__');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you do this, the thing to think about would be: since you're not doing the "redirect via authenticate.html" thing, the access_token is right in the URL bar:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-03-26_8-55-58.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/9467iF7F1B23DF5014F20/image-size/medium?v=v2&amp;amp;px=400" role="button" title="2021-03-26_8-55-58.png" alt="2021-03-26_8-55-58.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;which is not ideal, security-wise. So I would recommend using some JavaScript to remove that.&lt;/P&gt;</description>
    <pubDate>Fri, 26 Mar 2021 14:36:44 GMT</pubDate>
    <dc:creator>GavinRehkemper</dc:creator>
    <dc:date>2021-03-26T14:36:44Z</dc:date>
    <item>
      <title>Use the BeginOAuth2 without a popup</title>
      <link>https://community.esri.com/t5/arcgis-rest-js-questions/use-the-beginoauth2-without-a-popup/m-p/1038641#M1</link>
      <description>&lt;P&gt;I am trying to include user authentication into a webpage. I want to use `BeginOAuth2` method from the rest-js library.&lt;/P&gt;&lt;P&gt;I have found some examples with the property `popup` set to `true`, for example, &lt;A href="https://github.com/Esri/arcgis-rest-js/tree/master/demos/oauth2-browser" target="_blank"&gt;https://github.com/Esri/arcgis-rest-js/tree/master/demos/oauth2-browser&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I do not want to redirect the user to any other site, just open the authentication in the same window and then go back to the original page (storing the obtained token in the local storage). However, I can't figure out how to deal with the `redirect uri` in those settings. Do you have any exemplary code snippets which deal with the `popup` set to false? Also, do I also need to use the `CompleteOAuth2` method to get the token?&lt;/P&gt;</description>
      <pubDate>Fri, 19 Mar 2021 13:58:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-js-questions/use-the-beginoauth2-without-a-popup/m-p/1038641#M1</guid>
      <dc:creator>AgnieszkaRozniak</dc:creator>
      <dc:date>2021-03-19T13:58:00Z</dc:date>
    </item>
    <item>
      <title>Re: Use the BeginOAuth2 without a popup</title>
      <link>https://community.esri.com/t5/arcgis-rest-js-questions/use-the-beginoauth2-without-a-popup/m-p/1040870#M2</link>
      <description>&lt;P&gt;Hi, have you tried setting the "popup" property to false in the example (&lt;A href="https://github.com/Esri/arcgis-rest-js/blob/d174facfb9b73dfd207313019cf2858a012dc798/demos/oauth2-browser/index.html#L165" target="_self"&gt;here&lt;/A&gt;)? I think that should work fine - let us know if it's not working and we can try to fix.&lt;/P&gt;&lt;P&gt;Your redirect URI will be the page that has CompleteOauth2 function call in it - in the sample, that is called &lt;A href="https://github.com/Esri/arcgis-rest-js/blob/master/demos/oauth2-browser/authenticate.html" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 22:50:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-js-questions/use-the-beginoauth2-without-a-popup/m-p/1040870#M2</guid>
      <dc:creator>GavinRehkemper</dc:creator>
      <dc:date>2021-03-25T22:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: Use the BeginOAuth2 without a popup</title>
      <link>https://community.esri.com/t5/arcgis-rest-js-questions/use-the-beginoauth2-without-a-popup/m-p/1040951#M3</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/320"&gt;@GavinRehkemper&lt;/a&gt;&amp;nbsp;and thank you for your reply. Yes, I have set the "popup" property to false based on the examples you linked. I am not sure if I was clear enough with my explanation so let me ask again. How can I use the OAuth2 without using the&amp;nbsp;authenticate file? Can I implement the authentication in a single script? How do I deal with the `completeOAuth2` method then?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 07:16:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-js-questions/use-the-beginoauth2-without-a-popup/m-p/1040951#M3</guid>
      <dc:creator>AgnieszkaRozniak</dc:creator>
      <dc:date>2021-03-26T07:16:20Z</dc:date>
    </item>
    <item>
      <title>Re: Use the BeginOAuth2 without a popup</title>
      <link>https://community.esri.com/t5/arcgis-rest-js-questions/use-the-beginoauth2-without-a-popup/m-p/1041054#M4</link>
      <description>&lt;P&gt;Hi, yes you can do that. Simply move the arcgisRest.UserSession.completeOAuth2() call into the main page. The key is to detect if you're in the state where the access_token has been passed by the URL. One way to do it would be something like this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;  // Define a global session variable.
  let session = null;

  // ADDED: ---------------------------------
  const clientIdFromURL = new URLSearchParams(window.location.search).get('clientID');
  const parsedHash = new URLSearchParams(
    window.location.hash.substr(1) // skip the first char (#)
  );
  var accessTokenFromURL = parsedHash.get('access_token');

  // only call completeOAuth2 if we have the clientID and access token:
  if(clientIdFromURL &amp;amp;&amp;amp; accessTokenFromURL) {
    session = arcgisRest.UserSession.completeOAuth2({
      clientId: clientIdFromURL,
    });
    localStorage.setItem('__ARCGIS_REST_USER_SESSION__', session.serialize());
  }
  // END ADDED SECTION: ---------------------------------

  // Check to see if there is a serialized session in local storage.
  const serializedSession = localStorage.getItem('__ARCGIS_REST_USER_SESSION__');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you do this, the thing to think about would be: since you're not doing the "redirect via authenticate.html" thing, the access_token is right in the URL bar:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-03-26_8-55-58.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/9467iF7F1B23DF5014F20/image-size/medium?v=v2&amp;amp;px=400" role="button" title="2021-03-26_8-55-58.png" alt="2021-03-26_8-55-58.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;which is not ideal, security-wise. So I would recommend using some JavaScript to remove that.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 14:36:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-js-questions/use-the-beginoauth2-without-a-popup/m-p/1041054#M4</guid>
      <dc:creator>GavinRehkemper</dc:creator>
      <dc:date>2021-03-26T14:36:44Z</dc:date>
    </item>
  </channel>
</rss>

