Skip to content

Commit

Permalink
fix(FSADT1-1276): token refresh time is 90% of exp
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Mar 27, 2024
1 parent 76cf9cf commit cb84751
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion frontend/src/helpers/ForestClientUserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class ForestClientUserSession implements SessionProperties {
return this.user;
};

/**
* Loads the user information from the user token and updates the user object.
* If the user token is not available, sets the user object to undefined.
* @returns A Promise that resolves to void.
*/
loadUser = async (): Promise<void> => {
const { idToken } = await this.loadUserToken();
if (idToken) {
Expand Down Expand Up @@ -81,16 +86,22 @@ class ForestClientUserSession implements SessionProperties {
email: parsedUser.email,
...this.processName(parsedUser, parsedUser["custom:idp_name"]),
};
// add the user type to the authorities
this.authorities.push(`${provider}_USER`.toUpperCase());

// add the groups to the authorities
if (parsedUser["cognito:groups"]) {
if (parsedUser["cognito:groups"]) {
const groups: string[] | undefined = parsedUser["cognito:groups"];
groups?.forEach((group) => this.authorities.push(group));
}
}

// get the token expiration time, minus 10% to refresh the token before it expires
const timeDifference = ((idToken.payload.exp * 1000) - Date.now()) * 0.9;
// if the refresh interval is not set, set it
if (!this.sessionRefreshIntervalId){
this.sessionRefreshIntervalId = setInterval(() => this.loadUser(), 5 * 60 * 1000);
this.sessionRefreshIntervalId = setInterval(() => this.loadUser(), timeDifference);
}
} else {
this.user = undefined;
Expand Down

0 comments on commit cb84751

Please sign in to comment.