This tutorial demonstrates how to implement user authentication in your client applications using the ArcGIS
class in ArcGIS REST JS. This will allow users of your application to sign in with their ArcGIS account, granting your application access to the privileges and items associated with it.
Prerequisites
An ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account.
Steps
Create a new app
-
Create an
HTML
page called index.html an empty<body
. This page is the primary application page that directs user to the sign-in prompt.> index.htmlUse dark colors for code blocks <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" /> <title>ArcGIS REST JS: Sign in with user authentication (browser)</title> </head> <body> </body> </html>
-
Add HTML and CSS to create a
<pre
element called> result
.index.htmlUse dark colors for code blocks <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" /> <title>ArcGIS REST JS: Sign in with user authentication (browser)</title> <style> body { font-family: monospace; color: white; background: #000000; } pre { overflow: auto; padding: 1rem; background: #000000; } </style> </head> <body> <pre id="result"></pre> </body> </html>
-
In the same folder, create another HTML page called callback.html. This page is the redirect page after the user successfully signed in.
callback.htmlUse dark colors for code blocks <!DOCTYPE html> <html> <head> <title>ArcGIS REST JS: User authentication callback</title> </head> <body> </body> </html>
-
In both index.html and callback.html, add references to the
arcgis-rest-request
package.index.htmlUse dark colors for code blocks <!-- ArcGIS REST JS used for user authentication. --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script>
callback.htmlUse dark colors for code blocks <!-- ArcGIS REST JS used for user authentication. --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script>
Set up authentication
Create a new OAuth credential to register the application.
- Go to the Create OAuth credentials for user authentication tutorial to create an OAuth credential.
- Copy the Client ID and Redirect URL from your OAuth credentials item and paste them to a safe location. They will be used in a later step.
Create authentication script
-
In index.html, call
ArcGIS
to begin OAuth authentication. Pass yourIdentity Manager.begin O Auth2 client
andId redirect
. This method will open a new window at the authorization endpoint that prompts users to sign in with an ArcGIS account.Url index.htmlUse dark colors for code blocks <script type="module"> /* Use for user authentication */ const authentication = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials redirectUri: "YOUR_REDIRECT_URL", // The redirect URL registered in your OAuth credentials portal: "https://www.arcgis.com/sharing/rest" // Your portal URL }) </script>
Upon signing in successfully, this window will redirect the browser to the provided
redirect
address.Url
Create callback script
Once the user has signed in, the browser will be redirected to the address of the provided redirect URI.
-
In callback.html, call
ArcGIS
to complete OAuth authentication. Pass yourIdentity Manager.complete O Auth2 client
andId redirect
. This method receives the authorization code generated when a user signs in and uses it to request an access token.ssUrl callback.htmlUse dark colors for code blocks <script type="module"> arcgisRest.ArcGISIdentityManager.completeOAuth2({ clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials redirectUri: "YOUR_REDIRECT_URL", // The redirect URL registered in your OAuth credentials portal: "https://www.arcgis.com/sharing/rest" // Your portal URL }) </script>
Run the app
Run the application and navigate to your localhost, for example: https
. You should be prompted to enter the credentials of an ArcGIS account.
If you are unable to sign in, make sure you have the correct redirect URL and port. This URL varies based on your application and typically takes the format of https
or http
. For example, if you are running an application on http
, set http
as your redirect URL in the index.html and callback.html file and your developer credential. They all have to match!
What's next
Learn how to use additional ArcGIS location services in these tutorials: