Skip to content

Commit 4a27304

Browse files
committed
add forceRefresh option to getAccessToken method
1 parent 695dc0f commit 4a27304

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/create-client.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,38 @@ describe("create-client", () => {
522522
expect(accessToken).toEqual(expect.stringMatching(/\.eyJ/));
523523
scope.done();
524524
});
525+
526+
it("refreshes the access token when forceRefresh is true", async () => {
527+
// First create a client with a valid (non-expired) token
528+
const { scope: initialScope } = nockRefresh({
529+
accessTokenClaims: {
530+
jti: "initial-token",
531+
// Set future expiration
532+
exp: Date.now() / 1000 + 3600,
533+
},
534+
});
535+
536+
client = await createClient("client_123abc", {
537+
redirectUri: "https://example.com/",
538+
});
539+
initialScope.done();
540+
541+
// Setup a refresh response for the forced refresh
542+
const { scope: refreshScope } = nockRefresh({
543+
accessTokenClaims: {
544+
jti: "forced-refresh-token",
545+
},
546+
});
547+
548+
// Get the token with forceRefresh: true
549+
const accessToken = await client.getAccessToken({
550+
forceRefresh: true,
551+
});
552+
553+
// Verify the token was refreshed
554+
expect(getClaims(accessToken).jti).toEqual("forced-refresh-token");
555+
refreshScope.done();
556+
});
525557
});
526558

527559
describe("when the current session is not authenticated", () => {

src/create-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ export class Client {
140140
}
141141
}
142142

143-
async getAccessToken(): Promise<string> {
144-
if (this.#shouldRefresh()) {
143+
async getAccessToken(options?: { forceRefresh?: boolean }): Promise<string> {
144+
if (options?.forceRefresh || this.#shouldRefresh()) {
145145
try {
146146
await this.#refreshSession();
147147
} catch (err) {

0 commit comments

Comments
 (0)