How to get user logged in without passing through login dialog in javascript ?

511
0
12-27-2019 07:05 AM
LaurentDuruflé
New Contributor II

From sample provided here I would like to login using a specific login/password embedded in my javascript code without any login dialog (login will be transparent to user). Any idea how to customized the JS code from this sample to achieve this objective ?

 

sample using login dialog (from ArcGIS API for JavaScript Sandbox 😞

 

<script>

  var map, cred = "esri_jsapi_id_manager_data"; // cookie/local storage name

 

  require([

    "dojo/_base/unload",

    "dojo/cookie",

    "dojo/json",

    "dojo/parser", "esri/config", "esri/IdentityManager", "esri/layers/FeatureLayer",

    "esri/map",

    "dijit/layout/BorderContainer",

    "dijit/layout/ContentPane",

    "dojo/domReady!",

  ], function (baseUnload,

    cookie,

    JSON,

    parser, esriConfig, esriId, FeatureLayer,

    Map){

 

    // store credentials/serverInfos before the page unloads

    baseUnload.addOnUnload(storeCredentials);

 

    // look for credentials in local storage

    loadCredentials();

 

    parser.parse();

 

    esriConfig.defaults.io.proxyUrl = "/proxy/";

 

    map = new Map("mapCanvas", {

      basemap: "topo",

      center: [-120.723, 35.165],

      zoom: 12

    });

 

    //add the secure service - token is required

    var secureLayer = new FeatureLayer("https://sampleserver6.arcgisonline.com/arcgis/rest/services/SaveTheBay/FeatureServer/0",

      {

        mode: FeatureLayer.MODE_ONDEMAND,

        outFields: ["*"]

      });

    map.addLayer(secureLayer);

 

    function loadCredentials(){

      var idJson, idObject;

 

      if (supports_local_storage()) {

        // read from local storage

        idJson = window.localStorage.getItem(cred);

      }

      else {

        // read from a cookie

        idJson = cookie(cred);

      }

 

      if (idJson && idJson != "null" && idJson.length > 4) {

        idObject = JSON.parse(idJson);

        esriId.initialize(idObject);

      }

      else {

        // console.log("didn't find anything to load :(");

      }

    }

 

    function storeCredentials(){

      // make sure there are some credentials to persist

      if (esriId.credentials.length === 0) {

        return;

      }

 

      // serialize the ID manager state to a string

      var idString = JSON.stringify(esriId.toJson());

      // store it client side

      if (supports_local_storage()) {

        // use local storage

        window.localStorage.setItem(cred, idString);

        // console.log("wrote to local storage");

      }

      else {

        // use a cookie

        cookie(cred, idString, {expires: 1});

        // console.log("wrote a cookie :-/");

      }

    }

 

    function supports_local_storage(){

      try {

        return "localStorage" in window && window["localStorage"] !== null;

      } catch (e) {

        return false;

      }

    }

  });

  </script>

Tags (1)
0 Kudos
0 Replies