Sign in with user authentication (browser)

User authentication using a sign-in prompt.

This tutorial demonstrates how to implement in your client applications using the ArcGISIdentityManager class in ArcGIS REST JS. This will allow users of your application to sign in with their , granting your application access to the privileges and items associated with it.

Prerequisites

Steps

Create a new app

  1. 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.html
    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    <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>
  2. Add HTML and CSS to create a <pre> element called result.

    index.html
    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    <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>
  3. In the same folder, create another HTML page called callback.html. This page is the redirect page after the user successfully signed in.

    callback.html
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <!DOCTYPE html>
    
    <html>
    
    <head>
      <title>ArcGIS REST JS: User authentication callback</title>
    
    </head>
    
    <body>
    
    </body>
    
    </html>
  4. In both index.html and callback.html, add references to the arcgis-rest-request package.

    index.html
    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
      <!-- ArcGIS REST JS used for user authentication. -->
      <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script>
    
    Expand
    callback.html
    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
      <!-- ArcGIS REST JS used for user authentication. -->
      <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script>
    
    Expand

Set up authentication

Create a new to register the application.

  1. Go to the Create OAuth credentials for user authentication tutorial to create an OAuth credential.
  2. 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

  1. In index.html, call ArcGISIdentityManager.beginOAuth2 to begin OAuth authentication. Pass your clientId and redirectUrl. This method will open a new window at the that prompts users to sign in with an .

    index.html
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
      <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 redirectUrl address.

Create callback script

Once the user has signed in, the browser will be redirected to the address of the provided redirect URI.

  1. In callback.html, call ArcGISIdentityManager.completeOAuth2 to complete OAuth authentication. Pass your clientId and redirectUrl. This method receives the generated when a user signs in and uses it to request an .

    callback.html
    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
      <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>
    
    Expand

Run the app

Run the application and navigate to your localhost, for example: https://localhost:8080. You should be prompted to enter the credentials of an .

What's next

Learn how to use additional ArcGIS location services in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close