Access secure resources from ArcGIS Portal with ArcGIS API for javascript without login pop-up window

432
1
04-07-2020 01:42 AM
ShubhamTiwari
New Contributor II

I have recently started working on ArcGIS API's. I am able to add feature layers/ edit layers etc. However I want to validate user id and password using ArcGIS API's and generate token once the credentials are verified.
I tried couple of ways as below but could not accomplish the results.

```
function login(user, password)
{
var serverInfo = new ServerInfo();
serverInfo.server = 'https://myorganizatoon.arcgis.com/';
serverInfo.tokenServiceUrl="http://myorganization.arcgis.com/arcgis/tokens/generateToken";
//serverInfo.tokenServiceUrl=http://www.arcgis.com/arcgis/tokens/generateToken";
//(I don't know which of these two links are correct for the serverinfo, that's why I tried both. Please suggest me which one is correct?)
esriId.registerServers([serverInfo]);
var serverInfo = esriId.serverInfos[0];

var userInfo = {};
userInfo.username = "myusername";
userInfo.password = "mypassword";
esriId.generateToken(serverInfo, userInfo).then(function (response)

{
   response.server = serverInfo.server;
   response.userId = user;
   esriId.registerToken(response);
});
}
```

I am using ArcGIS API for javascript 3.31.
Please suggest.

0 Kudos
1 Reply
VictorCoelho
New Contributor III
0 Kudos