Getting Win User ID of app user in WAB (ignoring Portal)

450
3
09-01-2020 06:03 AM
Arne_Gelfert
Occasional Contributor III

So I would like to log some simple website/web application hits in a web app, log page, time, user name when someone accesses the app. 

This older thread here suggested doing:

console.log(IdentityManager.credentials[0].userId)

//returns <user@domain>

This works great when used inside one of my widgets, and I might use it there to figure out who actually uses them. But where would be the place to put this to log web app access before any widget's been activated? I haven't delved into the jimu/WAB code beyond life inside widgets. First attempt at putting it in something like main.js have failed and returned Null.

I'm not really wanting to go through Portal, as suggested here, where an app is shared with everyone and not accessed through Portal and necessarily registered there. But maybe registering it there regardless is the solution? Any guidance is appreciated.

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

Arne,

  In the ConfigManager.js you can get the user by adding this line (line 30):

    _onAppConfigSet: function(c){
      //summary:
      //  this method may be called by builder or UT
      c = jimuUtils.reCreateObject(c);

      window.appInfo.isRunInMobile = jimuUtils.inMobileSize();

      this.configLoader.processProxy(c);

      this.configLoader.addNeedValues(c);

      this.configLoader.loadAndUpgradeAllWidgetsConfig(c).then(lang.hitch(this, function(c){
        this._addDefaultValues(c);

        tokenUtils.setPortalUrl(c.portalUrl);
        window.portalUrl = c.portalUrl;

        if(this.appConfig){
          //remove the options that are relative to map's display when map is changed.
          jimuUtils.deleteMapOptions(c.map.mapOptions);
          this.appConfig = c;
          this._deleteDataSourcesFromMap();
          topic.publish('appConfigChanged', this.getAppConfig(), 'resetConfig', c);
        }else{
          this.appConfig = c;

          var portal = portalUtils.getPortal(c.portalUrl);
          portal.loadSelfInfo().then(lang.hitch(this, function(portalSelf){
            this.portalSelf = portalSelf;
            console.info(this.portalSelf.user.username);
            topic.publish("appConfigLoaded", this.getAppConfig());
          }));
        }
      }));
    },
0 Kudos
Arne_Gelfert
Occasional Contributor III

So it's ConfigManager.js - cool! I will try that. Thank you!

I need to go back and review some of the documentation on lifecycle and loading for WAB (to the extent that it's documented ). When I was starting out and staring down the abyss of building my first widget, it didn't make a heckuva lotta sense. Maybe now it will. 

0 Kudos
Arne_Gelfert
Occasional Contributor III

Still haven't had time to try this but I will report back after doing so eventually.