I am trying to retrieve the currently logged in user for a widget when the portal uses IWA. I have seen the following posts
How can I retrieve the portal user name in a custom WAB widget?
a programmatic way to get Portal groups and Portal members info within WAB
So I have the following
getUserName: function(item) {
var portal = portalUtils.getPortal(item.portalUrl);
if (portal == null ) return "Cannot Get Portal";
if ( portal.user == null ) return "No User";
let username = user.username;
return username;
},
This approach seems like it works.
The behavior I see is that the user that created the Web Application everything works as expected, the IWA logged in user is returned. However, if anyone else uses the widget the value returned is the "No User" string.
We have tested with different people. If I create the Web Application and run things it works. But another system uses the widget and the portal.user is returning null. If another user creates the Web Application it works for them, but if I run it (or anyone else) we are getting null for portal.user.
Anyone know what be might going on? Is something specific needed because of the IWA configuration?
Thanks
-Joe
Solved! Go to Solution.
So what I found is that a call to Portal.signIn method Portal (legacy) | API Reference | ArcGIS API for JavaScript 3.31 is required (don't ask me why). Because it is IWA there is no request for credentials made. So my previous method becomes:
getUserName: function(item) {
var deferred = new Deferred();
var portal = portalUtils.getPortal(item.portalUrl);
if (portal == null ) deferred.resolve('Cannot Get Portal');
portal.signIn().then(function(portalUser){
console.log(portalUser.userId);
deferred.resolve(portalUser.userId);
}, function(err){
console.log(err);
deferred.resolve('No User');
});
return deferred.promise
},
This does make things more complex because signIn returns a deferred so my calling method needs to use the Deferred:
this.getUserName(item).then(lang.hitch(this, function(username){
//code that needed user name
}));
So what I found is that a call to Portal.signIn method Portal (legacy) | API Reference | ArcGIS API for JavaScript 3.31 is required (don't ask me why). Because it is IWA there is no request for credentials made. So my previous method becomes:
getUserName: function(item) {
var deferred = new Deferred();
var portal = portalUtils.getPortal(item.portalUrl);
if (portal == null ) deferred.resolve('Cannot Get Portal');
portal.signIn().then(function(portalUser){
console.log(portalUser.userId);
deferred.resolve(portalUser.userId);
}, function(err){
console.log(err);
deferred.resolve('No User');
});
return deferred.promise
},
This does make things more complex because signIn returns a deferred so my calling method needs to use the Deferred:
this.getUserName(item).then(lang.hitch(this, function(username){
//code that needed user name
}));