Im trying to refresh an ArcServer token in the JS API, but can't seem to figure it out.
This gist is: I want to refresh the server token every hour automatically without requiring more input from the user while within the same session (ie. the user hasn't closed the browser window).
Right now I get the token with a basic POST request to the server and then register that token with the ID manager like this:
IdentityManager<SPAN class="punctuation token">.</SPAN><SPAN class="token function">registerToken</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN>
server<SPAN class="punctuation token">:</SPAN> "<SPAN class="comment token">//exampleUrl/server/rest/services/",</SPAN>
token<SPAN class="punctuation token">:</SPAN> localStorage<SPAN class="punctuation token">.</SPAN><SPAN class="token function">getItem</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'token'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>That's it and it works just fine, but when the token expires in an hour (which we want), I don't have a way to simply refresh the token. Since I'm not storing the password like I am the username, I can't simply fire the POST request again to generate a new token.
I've looked at creating a credential object which has a refreshToken() method, but can't get it to work, and to be honest I don't fully understand the credential class as it relates to the ID manager, serverInfos, and authentication in general. I tried replacing the above code to register the token with ID manager with the following code with no success:
<SPAN class="keyword token">var</SPAN> serverInfo <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">ServerInfo</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
serverInfo<SPAN class="punctuation token">.</SPAN>server <SPAN class="operator token">=</SPAN> "<SPAN class="comment token">//exampleUrl/server1";</SPAN>
serverInfo<SPAN class="punctuation token">.</SPAN>tokenServiceUrl <SPAN class="operator token">=</SPAN> <SPAN class="string token">"exmapleUrl/rest/generateToken"</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">let</SPAN> idObject <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>serverInfos<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN>serverInfo<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">let</SPAN> credential <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>
server<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"exampleUrl/server1/"</SPAN><SPAN class="punctuation token">,</SPAN>
ssl<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">,</SPAN>
token<SPAN class="punctuation token">:</SPAN> localStorage<SPAN class="punctuation token">.</SPAN><SPAN class="token function">getItem</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"token"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
userId<SPAN class="punctuation token">:</SPAN> localStorage<SPAN class="punctuation token">.</SPAN><SPAN class="token function">getItem</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"username"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">}</SPAN>
idObject<SPAN class="punctuation token">.</SPAN>credentials <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>credential<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN>
IdentityManager<SPAN class="punctuation token">.</SPAN><SPAN class="token function">initialize</SPAN><SPAN class="punctuation token">(</SPAN>idObject<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>(used this as a guide, but I'm not sure Jeff Pace was using the 4.x API)
Does anyone have any examples of using the refreshToken() method, or any insight into the best way to accomplish an automatic token refresh within the API?