Learn how to use ArcGIS Maps SDK for Javascript to implement user authentication.
If your application needs access to your users' secure content through ArcGIS or if you are distributing your app through ArcGIS Marketplace, you must implement user authentication. This allows individual users with an ArcGIS Online or ArcGIS Enterprise account to authorize your app to use the content and services to which they have access; it also uses their credits for any paid premium content and services.
Prerequisites
You need an ArcGIS account to register a new application and obtain its client. See the register your application tutorial. If you do not have an ArcGIS account you can sign up for a free ArcGIS Location Platform account.
When registering your application you will need to configure a redirect URL that will point to the URL where you are hosting your application. Generally this will be a local web server such as http.
Steps
Create a new pen
- Go to CodePen to create a new pen for your mapping application.
Add the HTML
- In CodePen > HTML, add HTML and CSS to create a page with
<buttonand> <preelements to allow the user to sign in, sign out, and to display user credentials.> Use dark colors for code blocks <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" /> <title>ArcGIS Maps SDK for JavaScript Tutorials: Implement user authentication</title> <style> html, body { font-size: 150%; margin: 10vh 10vw; } </style> </head> <body> <button id="sign-in" class="btn btn-primary">Sign In</button> <button id="sign-out" class="btn btn-primary">Sign Out</button> <pre><code id="results"></code></pre> </body> </html>
Reference the API
- In the
<headtag, add references to the CSS file and JS library.> Use dark colors for code blocks <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" /> <title>ArcGIS Maps SDK for JavaScript Tutorials: Implement user authentication</title> <link rel="stylesheet" href="https://js.arcgis.com/4.34/esri/themes/light/main.css" /> <script src="https://js.arcgis.com/4.34/"></script> <style> html, body { font-size: 150%; margin: 10vh 10vw; } </style> </head>
Add modules
-
In a new
<scriptat the bottom of the> <body, use> $arcgis.importto add thePortal,O, andAuth Info Identitymodules.Manager The ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The
$arcgis.importglobal function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK's different modules, visit the References page.Use dark colors for code blocks <script type="module"> const [Portal, OAuthInfo, esriId] = await $arcgis.import([ "@arcgis/core/portal/Portal.js", "@arcgis/core/identity/OAuthInfo.js", "@arcgis/core/identity/IdentityManager.js", ]); </script>
Register credentials with Identity Manager
-
Go to the OAuth credentials item page and copy the Client ID.
-
Create an
Oobject and set theAuth Info appwith the copiedId clientbefore you register it with_id Identity.Manager Use dark colors for code blocks const [Portal, OAuthInfo, esriId] = await $arcgis.import([ "@arcgis/core/portal/Portal.js", "@arcgis/core/identity/OAuthInfo.js", "@arcgis/core/identity/IdentityManager.js", ]); const info = new OAuthInfo({ appId: "YOUR-CLIENT-ID", popup: false, // the default }); esriId.registerOAuthInfos([info]);
Handle sign in
Once you have registered your client with Identity the ArcGIS Maps SDK for JavaScript will automatically prompt a user to authorize your application whenever it accesses a service that requires authentication. Create a sign in experience by accessing the users profiles with the Portal class.
-
Create a
handlefunction to be called when the user authorizes your application and then load theSigned In Portal. After the user provides authorization, obtain and display thefullandName usernameon the page.Use dark colors for code blocks const info = new OAuthInfo({ appId: "YOUR-CLIENT-ID", popup: false, // the default }); esriId.registerOAuthInfos([info]); function handleSignedIn() { const portal = new Portal(); portal.load().then(() => { const results = { name: portal.user.fullName, username: portal.user.username }; document.getElementById("results").innerText = JSON.stringify(results, null, 2); }); } -
Call the
checkmethod against a service URL. If the user has provided credentials, call theSign In Status handlefunction.Signed In checkcan accept a URL for any service. The default ArcGIS portal URLSign In Status httpsis the easiest way to fully authenticate a user.://arcgis.com/sharing/rest/ Use dark colors for code blocks const info = new OAuthInfo({ appId: "YOUR-CLIENT-ID", popup: false, // the default }); esriId.registerOAuthInfos([info]); esriId .checkSignInStatus(info.portalUrl + "/sharing") .then(() => { handleSignedIn(); }) function handleSignedIn() { const portal = new Portal(); portal.load().then(() => { const results = { name: portal.user.fullName, username: portal.user.username }; document.getElementById("results").innerText = JSON.stringify(results, null, 2); }); }
Handle sign out
-
Create a
handlefunction when a user's credentials are destroyed.Signed Out Use dark colors for code blocks function handleSignedIn() { const portal = new Portal(); portal.load().then(() => { const results = { name: portal.user.fullName, username: portal.user.username }; document.getElementById("results").innerText = JSON.stringify(results, null, 2); }); } function handleSignedOut() { document.getElementById("results").innerText = "Signed Out"; } -
Append a
catchstatement to call thehandlefunction when the user signs out.Signed Out Use dark colors for code blocks esriId .checkSignInStatus(info.portalUrl + "/sharing") .then(() => { handleSignedIn(); }) .catch(() => { handleSignedOut(); });
Add event listeners
-
Call the
getmethod when a user clicks theCredential sign-inbutton.Use dark colors for code blocks .catch(() => { handleSignedOut(); }); document.getElementById("sign-in").addEventListener("click", function () { esriId.getCredential(info.portalUrl + "/sharing"); }); -
Call the
destroymethod when a user clicks theCredentials sign-outbutton before reloading the page.Use dark colors for code blocks .catch(() => { handleSignedOut(); }); document.getElementById("sign-in").addEventListener("click", function () { esriId.getCredential(info.portalUrl + "/sharing"); }); document.getElementById("sign-out").addEventListener("click", function () { esriId.destroyCredentials(); window.location.reload(); });
Run the App
You should now have an application that can check for credentials using OAuth 2.0.
What's next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: