Skip to content

Commit a6c9b75

Browse files
author
vineet kumar
authored
Merge pull request #1579 from commercetools/1528-How-to-use-node-sdk-auth-to-get
fix(sdk-auth): Invalidate cache even after promise reject
2 parents 0e9ca25 + 0bf0de7 commit a6c9b75

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

integration-tests/sdk/auth.it.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,42 @@ describe('Auth Flows', () => {
286286
)
287287
}
288288
})
289+
290+
it('should tokenProvider work even after changing fetchTokenInfo funtion', async () => {
291+
const onTokenInfoRefreshedMock = jest.fn()
292+
293+
const tokenProvider = new TokenProvider(
294+
{
295+
sdkAuth: authClient,
296+
onTokenInfoRefreshed: onTokenInfoRefreshedMock,
297+
}
298+
)
299+
tokenProvider.fetchTokenInfo = (sdkAuth) =>
300+
sdkAuth.refreshTokenFlow(encodeURIComponent("invalid refresh token"));
301+
302+
try {
303+
await tokenProvider.getAccessToken()
304+
} catch (err) {
305+
expect(err.toString()).toEqual(
306+
expect.stringContaining(
307+
'BadRequest: The refresh token was not found. It may have expired.'
308+
)
309+
)
310+
tokenProvider.fetchTokenInfo = (sdkAuth) =>
311+
sdkAuth.anonymousFlow();
312+
const tokenInfo = await tokenProvider.getTokenInfo();
313+
314+
// check returned properties
315+
expect(tokenInfo).toHaveProperty('access_token')
316+
expect(tokenInfo.scope).toMatch(
317+
`manage_project:${projectKey} anonymous_id`
318+
)
319+
expect(tokenInfo).toHaveProperty('expires_in')
320+
expect(tokenInfo).toHaveProperty('expires_at')
321+
expect(tokenInfo).toHaveProperty('refresh_token')
322+
expect(tokenInfo).toHaveProperty('token_type', 'Bearer')
323+
}
324+
})
289325
})
290326

291327
describe('Error cases', () => {

packages/sdk-auth/src/tokenProvider.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export default class TokenProvider {
7979
return this.fetchTokenInfoPromise.then((tokenInfo) => {
8080
this.fetchTokenInfoPromise = null
8181
return tokenInfo
82+
}).catch((error) => {
83+
this.fetchTokenInfoPromise = null
84+
throw error
8285
})
8386
}
8487

@@ -91,6 +94,9 @@ export default class TokenProvider {
9194
return this.refreshTokenFlowPromise.then((refreshTokenInfo) => {
9295
this.refreshTokenFlowPromise = null
9396
return refreshTokenInfo
97+
}).catch((error) => {
98+
this.refreshTokenFlowPromise = null;
99+
throw error
94100
})
95101
}
96102

packages/sdk-auth/test/token-provider.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@ describe('Token Provider', () => {
4141
)
4242
})
4343

44+
test('should clear cache after _performFetchTokenInfo rejects', async () => {
45+
const _tokenProvider = new TokenProvider({
46+
sdkAuth,
47+
fetchTokenInfo: jest
48+
.fn()
49+
.mockImplementation(() => Promise.reject(new Error("Invalid Token"))),
50+
})
51+
expect(_tokenProvider.fetchTokenInfoPromise).toBeFalsy();
52+
try {
53+
await _tokenProvider._performFetchTokenInfo()
54+
} catch(e) {
55+
expect(_tokenProvider.fetchTokenInfoPromise).toBeFalsy();
56+
}
57+
})
58+
test('should clear cache after _performRefreshTokenFlow rejects', async () => {
59+
const _tokenProvider = new TokenProvider({
60+
sdkAuth,
61+
fetchTokenInfo: jest
62+
.fn()
63+
.mockImplementation(() => Promise.reject(new Error("Invalid Token"))),
64+
})
65+
expect(_tokenProvider.refreshTokenFlowPromise).toBeFalsy();
66+
try {
67+
await _tokenProvider._performRefreshTokenFlow("invalid refresh token")
68+
} catch(e) {
69+
expect(_tokenProvider.refreshTokenFlowPromise).toBeFalsy();
70+
}
71+
})
72+
4473
test('should throw an error when refreshing without "refresh_token" property', async () => {
4574
const _tokenProvider = new TokenProvider(
4675
{ sdkAuth },

0 commit comments

Comments
 (0)