Skip to content

Commit fed02f7

Browse files
committed
Refresh authToken when it expires
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]>
1 parent eb4fb69 commit fed02f7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/classes/AuthenticationSession.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ enum FusionAuthStatusCode {
1515
export class AuthenticationSession {
1616
public authToken = '';
1717

18+
public refreshToken = '';
19+
1820
public readonly authContext;
1921

2022
private readonly fusionAuthClient;
@@ -57,6 +59,7 @@ export class AuthenticationSession {
5759
username: this.authContext.username,
5860
});
5961
this.authToken = clientResponse.response.token;
62+
this.refreshToken = clientResponse.response.refreshToken ?? '';
6063
this.authContext.accept();
6164
return;
6265
}
@@ -178,4 +181,18 @@ export class AuthenticationSession {
178181
this.authContext.reject();
179182
});
180183
}
184+
185+
private obtainNewAuthTokenUsingRefreshToken(): void {
186+
this.fusionAuthClient.exchangeRefreshTokenForAccessToken(this.refreshToken, '', '', '', '')
187+
.then((clientResponse) => {
188+
this.authToken = clientResponse.response.access_token ?? '';
189+
})
190+
.catch((clientResponse: unknown) => {
191+
const message = isPartialClientResponse(clientResponse)
192+
? clientResponse.exception.message
193+
: '';
194+
logger.warn(`Error obtaining refresh token : ${message}`);
195+
this.authContext.reject();
196+
});
197+
}
181198
}

0 commit comments

Comments
 (0)