Skip To ContentArcGIS for DevelopersSign In Dashboard


You will learn: how to build an app that uses named user login credentials to access secure services.

Applications can implement several authentication patterns based on OAuth 2.0 to accessArcGIS Onlinepremium services and secure content. There are two workflows that applications can use to authenticate with ArcGIS:

  • Named user login allows users of ArcGIS Online to use their ArcGIS Online account to log into your application are similar to signing into an app with your Google and Facebook account.
  • App login allows your application to authenticate with Arcgis Online and obtain credentials to access premium services for users without ArcGIS Online accounts.

You can learn more about implementing named user logins and implementing app logins in the authentication patterns documentation.

In this tutorial you will implement OAuth 2.0 to allow users to sign in with their ArcGIS Online account so they can access the ArcGIS Traffic Layer service. Unlike our other JavaScript tutorials, you won't be able to use CodePen and you'll have to run the code locally.


Pre-configure OAuth 2.0 for your app

  1. Sign in to your ArcGIS account. If you don't already have one, sign-up for free.

  2. At the top right of the main menu, click > New Application > Create New Application

    • Title: Access Premium Services App
    • Tags: OAuth
    • Description: Access Premium Services App
    • Click Register New Application
  3. Click the Authentication tab add localhost or your server name for the redirect URI:

    • Redirect URI http://localhost/
  4. Copy the Client ID value as you'll need this in the next step.

NOTE: The terms Client ID and App ID are used interchangeably in this tutorial.

Create your app's HTML

  1. Instead of using CodePen, create a file in your //localhost web directory and add the following code to create a basic starter app.

    <!DOCTYPE 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 API for JavaScript Tutorials: Access traffic using OAuth2</title>
        <link rel="stylesheet" href="" />
        <script src=""></script>
          #viewDiv {
            font-family: Avenir Next W00;
            font-size: 14px;
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
          .action {
            color: blue;
            cursor: pointer;
            text-decoration: underline;
        <div id="anonymousPanel" style="display: block; padding: 5px; text-align: center;">
          <span id="sign-in" class="action">Sign In</span> to see live traffic.
        <div id="personalizedPanel" style="display: none; padding: 5px; text-align: center;">
          Welcome <span id="userId" style="font-weight: bold;"></span> &nbsp;-&nbsp;
          <span id="sign-out" class="action">Sign Out</span>
        <!-- for the map -->
        <div id="viewDiv" style="display: none;"></div>

Add JavaScript to send users to to sign in

In order for users to authenticate themselves, you need to launch the OAuth login dialog. Execute the dialog when the "Sign in" button is clicked.

  1. In the file <head>, add a <script> tag and the JavaScript below. Specify the portalUrl and the appId for the OAuthInfo. Be sure to substitute your Client ID value created above for the appId. Use the identifityManager static class to register the info and then set up click listeners to get and destroy the credentials. Use checkSignInStatus method to show indicate when the user is signed in or signed out.

        ], function(
            Portal, OAuthInfo, identityManager, Map, MapView, MapImageLayer,
            domStyle, domAttr, on, dom) {
          // ArcGIS Online or your portal address
          var portalUrl = "";
          // subsitute your own client ID to identify who spawned the login and check for a matching redirect URI
          var info = new OAuthInfo({
            appId: "JTpyML5GgvA1jEoo", //*** Your Client ID value goes here ***//
            popup: false // inline redirects don't require any additional app configuration
          // send users to to login
          on(dom.byId("sign-in"), "click", function() {
          // log out and reload
          on(dom.byId("sign-out"), "click", function() {
          identityManager.checkSignInStatus(portalUrl).then(function() {
           dom.byId('anonymousPanel').style.display = 'none';
           dom.byId('personalizedPanel').style.display = 'block'
  2. Run the http://localhost/privatelayer.html page and try to sign-in and sign-out.

Create a map and add the Traffic Service layer after login is complete

The ArcGIS Traffic service is a premium service that can be used to visualize real time traffic speeds and incidents such as accidents, construction sites, or street closures. The service is free, but it does require applications to provide authentication to access it. The layer must be loaded after a user has been successfully authenticated.

  1. In the file, create a new function called displayMap that creates and adds the secure traffic layer to the map after the portal has been successfully loaded.

    function displayMap() {
      var portal = new Portal();
      // Once the portal has loaded, the user is signed in
      portal.load().then(function () {
        dom.byId("viewDiv").style.display = "flex";
        var map = new Map({
          basemap: "topo-vector"
        var view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 14,
          center: [-118.24, 34.05]
        var traffic = new MapImageLayer({
          url: ""
  2. Update the existing checkSignInStatus method to call the displayMap function. This will occur after a user has successfully signed in and is authenticated.

    identityManager.checkSignInStatus(portalUrl).then(function () {
      dom.byId("anonymousPanel").style.display = "none";
      dom.byId("personalizedPanel").style.display = "block";
      //*** ADD ***//
  3. Run the code. The map should display live traffic in Los Angeles after you sign in.

Congratulations, you're done!

Your app should look something like this.


Use a service proxy to access the traffic service

If you would like to use the "App login" authentication pattern to access the ArcGIS Traffic service directly and prevent the application from prompting the user to sign in, you can use a proxy service.

  1. Go to the Set up authenticated services lab and create a proxy to access the ArcGIS Traffic Layer Service. NOTE: To set up the proxy you can use the application created in the first step or you can create a new application for all service proxies.
  2. In the dashboard, copy the Traffic Layer proxy URL and update the application code.
    var traffic = new MapImageLayer({
      //*** ADD ***//
  3. In your app, comment out all of the OAuth code and call the displayMap function when the application loads.
  4. Run the application again and it shouldn't prompt the user to sign in.