Renew Access Token
### Request a renewal for an existing access token.
Extends the current session without creating a new one. Using this function in a long-running application will ensure that you don't start a new session unless it is 100% necessary. This will prevent your application from kicking itself or dropping dependent services that share an Access Token. See the [`/auth/accessTokenRequest`](#operation/accessTokenRequest) operation for more details.
```js
const { accessToken, expirationTime } = await fetch(TRADOVATELIVE+'/auth/accessTokenRequest', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(myCredentials) //see auth/accessTokenRequest for more info on the request body
}).then(res => res.json());
sessionStorage.setItem(CURRENT_TOKEN_KEY, accessToken);
sessionStorage.setItem(TOKEN_EXPIRATION_KEY, expirationTime);
//in another file or function, keep the connection alive
//check every 2 mins using useInterval (WARNING on a browser
//this fn will be throttled. Consider using a WebWorker to maintain
//a connection even when tabbed away.)
setInterval(() => {
const expiration = sessionStorage.getItem(TOKEN_EXPIRATION_KEY);
const now = new Date().getTime();
const exp = new Date(expiration).getTime();
//if expiration is within 15 mins, renew
if(exp - now < 15 * 60000) {
const currentToken = sessionStorage.getItem(CURRENT_TOKEN_KEY);
const renewal = await fetch(TRADOVATELIVE+'/auth/renewAccessToken', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${currentToken}` //send existing non-expired token
}
});
//set new token and expiration
sessionStorage.setItem(CURRENT_TOKEN_KEY, renewal.accessToken);
sessionStorage.setItem(TOKEN_EXPIRATION_KEY, renewal.expirationTime);
}
}, 120 * 60000)
```
Authentication
AuthorizationBearer
Bearer authentication of the form Bearer <token>, where token is your auth token.
Response
AccessTokenResponse
errorText
Non-empty if the request failed
accessToken
expirationTime
passwordExpirationTime
userStatus
Active, Closed, Initiated, TemporaryLocked, UnconfirmedEmail
Allowed values:
userId
name
hasLive

