Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(commercetools)!: refactor token handling in commercetools (#…
- Loading branch information
1 parent
965c5e9
commit 3073946
Showing
60 changed files
with
1,956 additions
and
1,715 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/commercetools/api-client/__tests__/api/isLoggedIn.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import isLoggedIn from '../../src/api/isLoggedIn'; | ||
|
||
const getMockContext = () => ({ | ||
client: { | ||
tokenProvider: true | ||
}, | ||
config: { | ||
handleIsLoggedIn: null, | ||
auth: { | ||
onTokenRead: null | ||
} | ||
} | ||
}); | ||
|
||
describe('[commercetools-api-client] isLoggedIn', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('defaults to false', () => { | ||
const context = getMockContext(); | ||
context.config.auth.onTokenRead = jest.fn(); | ||
|
||
expect(isLoggedIn(context)).toBeFalsy(); | ||
}); | ||
|
||
it('calls "handleIsLoggedIn" from config', () => { | ||
const context = getMockContext(); | ||
context.config.handleIsLoggedIn = jest.fn().mockImplementation(() => true); | ||
|
||
expect(isLoggedIn(context)).toBeTruthy(); | ||
expect(context.config.handleIsLoggedIn).toBeCalled(); | ||
}); | ||
|
||
it('returns false if visitor is a guest', () => { | ||
const context = getMockContext(); | ||
context.config.auth.onTokenRead = jest.fn().mockImplementation(() => ({ scope: '' })); | ||
|
||
expect(isLoggedIn(context)).toBeFalsy(); | ||
expect(context.config.auth.onTokenRead).toBeCalled(); | ||
}); | ||
|
||
it('returns false if visitor has anonymous session', () => { | ||
const context = getMockContext(); | ||
context.config.auth.onTokenRead = jest.fn().mockImplementation(() => ({ scope: 'anonymous_id' })); | ||
|
||
expect(isLoggedIn(context)).toBeFalsy(); | ||
expect(context.config.auth.onTokenRead).toBeCalled(); | ||
}); | ||
|
||
it('returns true if visitor has user session', () => { | ||
const context = getMockContext(); | ||
context.config.auth.onTokenRead = jest.fn().mockImplementation(() => ({ scope: 'customer_id' })); | ||
|
||
expect(isLoggedIn(context)).toBeTruthy(); | ||
expect(context.config.auth.onTokenRead).toBeCalled(); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/commercetools/api-client/__tests__/api/setShippingMethod.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/commercetools/api-client/__tests__/helpers/actions/updateShippingDetails.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/commercetools/api-client/src/api/accessToken/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Context } from '@vue-storefront/core'; | ||
import { createSdkHelpers } from '../../links/sdkHelpers'; | ||
|
||
const accessToken = (context: Context, isServer: boolean) => { | ||
const configuration = context.config; | ||
|
||
if (isServer) { | ||
// Don't add a cookie to the response if the endpoint was called during SSR | ||
configuration.auth.onTokenChange = () => {}; | ||
} | ||
|
||
const { tokenProvider } = createSdkHelpers(context.config); | ||
|
||
return tokenProvider.getTokenInfo(); | ||
}; | ||
|
||
export default accessToken; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/commercetools/api-client/src/api/applyCartCoupon/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 2 additions & 8 deletions
10
packages/commercetools/api-client/src/api/customerSignOut/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
const customerSignOut = async ({ config, client }) => { | ||
if (config.auth.onTokenRemove) { | ||
config.auth.onTokenRemove(); | ||
} | ||
|
||
if (client.tokenProvider) { | ||
client.tokenProvider.invalidateTokenInfo(); | ||
} | ||
const customerSignOut = async ({ config }) => { | ||
config.auth.onTokenRemove?.(); | ||
}; | ||
|
||
export default customerSignOut; |
2 changes: 1 addition & 1 deletion
2
packages/commercetools/api-client/src/api/customerUpdateMe/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
import { isAnonymousSession, isUserSession } from '../../helpers/utils'; | ||
|
||
const isGuest = (context) => { | ||
const { client, config } = context; | ||
const { config } = context; | ||
|
||
if (config.handleIsGuest) { | ||
if (typeof config.handleIsGuest === 'function') { | ||
return config.handleIsGuest(context); | ||
} | ||
|
||
if (client.tokenProvider || context.isProxy) { | ||
const token = config.auth.onTokenRead(); | ||
return !isAnonymousSession(token) && !isUserSession(token); | ||
} | ||
|
||
return false; | ||
const token = context.config.auth.onTokenRead(); | ||
return !isAnonymousSession(token) && !isUserSession(token); | ||
}; | ||
|
||
export default isGuest; |
13 changes: 13 additions & 0 deletions
13
packages/commercetools/api-client/src/api/isLoggedIn/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { isUserSession } from '../../helpers/utils'; | ||
|
||
const isLoggedIn = (context) => { | ||
const { config } = context; | ||
|
||
if (typeof config.handleIsLoggedIn === 'function') { | ||
return config.handleIsLoggedIn(context); | ||
} | ||
|
||
return isUserSession(context.config.auth.onTokenRead()); | ||
}; | ||
|
||
export default isLoggedIn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.