Hi Bhavin,
I would suggest generating a token with a longer life if you can. This is by far the easiest way to go about keeping a custom "session" active.
However, if you absolutely must keep the solution running beyond the lifespan of a token that you manually added, then read-on. Please note however that this is a hack and is not supported. I make no warranty of fitness or completeness of any kind, express or implied; proceed at your own risk.
The first thing to understand is that the token is stored in multiple places, all of which are maintained by the Identity Manager. Tokens are stored in the Identity Manager for new layers, and is stored in each layer for use in new queries. This is in addition to any tasks (e.g. QueryTask) that you have setup. If you want to continue using the map after the token has expired, you will need to supply a new token to these objects. Otherwise, attempting to update any existing layers or add a new layer will cause a login popup to show up.
Secondly, the Identity Manager will automatically attempt to refresh your token once it expires through a timer.
The code below applies to the 2.x API.
Updating the Identity Manager's Token
You can access the token, expires, and creationTime properties of your credentials to update them. An example is shown below:
IdentityManager.credentials.forEach(function(creds, i) {
if (creds.server === myServer) {
IdentityManager.credentials[i].token = result.token;
IdentityManager.credentials[i].expires = result.expires;
IdentityManager.credentials[i].creationTime = result.creationTime;
}
});
Cancel the automated refresh of the credentials
Each credential item in the Identity Manager has a timeout which will trigger a token refresh upon expiration. Below is an example of how to cancel this timer.
IdentityManager.credentials.forEach(function(creds, i) {
if (creds.server === myServer) {
clearTimeout(creds._refreshTimer);
}
});
Update tokens in layers
Updating the Identity Manager does not update the layers (and tasks), so you have to do that manually.
var layer = map.getLayer("myLayerID");
layer.credential.token = result.token;
layer.credentials.expires = result.expires;
layer.credentials.creationTime = result.creationTime;