IdentifyManager - Sign In dialog at client side in WAB application ?

587
0
09-26-2018 09:04 PM
MayurDodiya
Occasional Contributor

HI,

I created app using WAB 2.8 developer edition, downloaded and hosted on my web server. It working fine.

I want to use this code Identity Manager with info persisted client side | ArcGIS API for JavaScript 3.25  in app, store token at client side, and show Sign In dialog automatically after 1 hour token expires.

I do not want to use proxy as my requirement is to have token based authentication - display Sign In dialog when you open the app and Sign In dialog automatically get prompted when token expires ? 

Where should I add below code in WAB app ? 

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",
    "dojo/domReady!",
  ], function (baseUnload,
    cookie,
    JSON,
    parser, esriConfig, esriId
   ){

    // store credentials/serverInfos before the page unloads
    baseUnload.addOnUnload(storeCredentials);

    // look for credentials in local storage
    loadCredentials();

    parser.parse();

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

    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;
      }
    }
  });

Thanks.!!

0 Kudos
0 Replies