Skip to content

Commit

Permalink
Refresh authToken when it expires
Browse files Browse the repository at this point in the history
Access tokens have lifetimes that might end before the user wants to end
their authenticated session. Hence, we need to refresh the user access token
after an expiry detection.

Resolves: #175

Signed-off-by: fenn-cs <[email protected]>
  • Loading branch information
nfebe committed Jun 20, 2023
1 parent eb4fb69 commit fed02f7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/classes/AuthenticationSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ enum FusionAuthStatusCode {
export class AuthenticationSession {
public authToken = '';

public refreshToken = '';

public readonly authContext;

private readonly fusionAuthClient;
Expand Down Expand Up @@ -57,6 +59,7 @@ export class AuthenticationSession {
username: this.authContext.username,
});
this.authToken = clientResponse.response.token;
this.refreshToken = clientResponse.response.refreshToken ?? '';
this.authContext.accept();
return;
}
Expand Down Expand Up @@ -178,4 +181,18 @@ export class AuthenticationSession {
this.authContext.reject();
});
}

private obtainNewAuthTokenUsingRefreshToken(): void {
this.fusionAuthClient.exchangeRefreshTokenForAccessToken(this.refreshToken, '', '', '', '')
.then((clientResponse) => {
this.authToken = clientResponse.response.access_token ?? '';
})
.catch((clientResponse: unknown) => {
const message = isPartialClientResponse(clientResponse)
? clientResponse.exception.message
: '';
logger.warn(`Error obtaining refresh token : ${message}`);
this.authContext.reject();
});
}
}

0 comments on commit fed02f7

Please sign in to comment.