This tutorial shows how to implement user authenticationArcGIS object. This will allow users of your application to sign in with their ArcGIS account
Prerequisites
An ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account.
Steps
Create a new app
-
Create an
HTMLpage 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 lang="en"> <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
<preelement called> result.index.htmlUse dark colors for code blocks <html lang="en"> <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> -
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 lang="en"> <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-requestpackage.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 credentialclient_id, client_secret, and redirect URIs. They are a type of developer credential.
- Go to the Create OAuth credentials for user authentication tutorial to create an OAuth credential. Set the redirect URL to
https, for example:// <YOUR _SERVER >[ :YOUR _POR T]/callback.html https.://localhost :3000/callback.html - 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
ArcGISto begin OAuth authentication. Pass yourIdentity Manager.begin O Auth2 clientandId redirect. This method will open a new window at the authorization endpointUrl An authorization endpoint is an ArcGIS portal service endpoint that can be queried to request an authorization code. It is used to implement user authentication OAuth2.0 flows. that prompts users to sign in with an ArcGIS accountAn ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. .index.htmlUse dark colors for code blocks <script type="module"> /* Use for user authentication */ const clientId = "YOUR_CLIENT_ID"; // Your client ID from OAuth credentials const redirectUri = ""YOUR_REDIRECT_URI""; // The redirect URL registered in your OAuth credentials const authentication = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ clientId, redirectUri, portal: "https://www.arcgis.com/sharing/rest" // Your portal URL }); </script>Upon signing in successfully, this window will redirect the browser to the provided
redirectaddress.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
ArcGISto complete OAuth authentication. Pass yourIdentity Manager.complete O Auth2 clientandId redirect. This method receives the authorization codeUrl An authorization code is a string that is used to obtain an access token associated with a user account. It is required in user authentication flows that adhere to the OAuth2.0 generated when a user signs in and uses it to request an access tokenauthorization_codegrant type.An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. .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_URI", // 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 location services in these tutorials: