Authentication using OAutho2.0 in ArcGIS API Javascrippt

468
4
11-01-2022 10:51 AM
TheGamer
Occasional Contributor

Hi everyone, so I have an Organization ArcGIS online account with a creator user type. I'm trying to access the hosted feature layer stored in the account and I need help with authenticating and identifying the app.

I'm having trouble with my code,  when I click on the sign-in button, nothing happens.  For the OAuth 2.0 application which I created, I have set the redirect URL to http://localhost:3000 . Also, is there a way to make the sign-in button disappear after the user has signed in?

0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor

"document.getElementbyID" is misspelled, so that event listener is never applied. You have to use the correct case in JavaScript, so that should be "document.getElementById". Make sure you check the browser's developer tools to catch mistakes like this.

You can use code like this to hide or display elements

 

      document.getElementById("sign-in").addEventListener("click", () => {
        document.getElementById("sign-in").style.display = "none";
        document.getElementById("sign-out").style.display = "block";
        esriId.getCredential(info.portalUrl + "/sharing");
      });
      document.getElementById("sign-out").addEventListener("click", () => {
        esriId.destroyCredentials();
        document.getElementById("sign-in").style.display = "block";
        document.getElementById("sign-out").style.display = "none";
      });

 

 

0 Kudos
TheGamer
Occasional Contributor

Hi @KenBuja omgg thank you so much! I fixed the error but when i click on it, it says www.arcgis.com refused to connect

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Have you read the developer help about setting up authentication?

0 Kudos
KenBuja
MVP Esteemed Contributor

One more thing...take a look at a recent blog that @ReneRubalcava3 wrote about authentication code in JavaScript.

0 Kudos