Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Override web component sdk configs (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
asafshen authored Feb 5, 2024
1 parent 2c01b28 commit d7dcfa5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 34 deletions.
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"dependencies": {
"@descope/user-management-widget": "0.0.7",
"@descope/web-component": "3.7.0"
"@descope/web-component": "3.7.6"
},
"peerDependencies": {
"vue": ">=3"
Expand Down
22 changes: 21 additions & 1 deletion src/Descope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,28 @@ import { useOptions, useDescope } from './hooks';
import { baseHeaders } from './constants';
import { RequestConfig } from '@descope/core-js-sdk';
import { computed } from 'vue';
import { getGlobalSdk } from './sdk';

DescopeWcClass.sdkConfigOverrides = { baseHeaders };
DescopeWcClass.sdkConfigOverrides = {
// Overrides the web-component's base headers to indicate usage via the React SDK
baseHeaders,
// Disables token persistence within the web-component to delegate token management
// to the global SDK hooks. This ensures token handling aligns with the SDK's configuration,
// and web-component requests leverage the global SDK's beforeRequest hooks for consistency
persistTokens: false,
hooks: {
get beforeRequest() {
// Retrieves the beforeRequest hook from the global SDK, which is initialized
// within the AuthProvider using the desired configuration. This approach ensures
// the web-component utilizes the same beforeRequest hooks as the global SDK
return getGlobalSdk().httpClient.hooks.beforeRequest;
},
set beforeRequest(_) {
// The empty setter prevents runtime errors when attempts are made to assign a value to 'beforeRequest'.
// JavaScript objects default to having both getters and setters
}
}
};

const props = defineProps({
flowId: {
Expand Down
19 changes: 10 additions & 9 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IS_BROWSER } from './constants';
import { wrapInTry } from './utils';

type Sdk = ReturnType<typeof createSdkWrapper>;
let sdkInstance: Sdk;
let globalSdk: Sdk;

const createSdkWrapper = <P extends Parameters<typeof createSdk>[0]>(
config: P
Expand All @@ -13,7 +13,7 @@ const createSdkWrapper = <P extends Parameters<typeof createSdk>[0]>(
persistTokens: IS_BROWSER as true,
autoRefresh: IS_BROWSER as true
});
sdkInstance = sdk;
globalSdk = sdk;

return sdk;
};
Expand All @@ -25,11 +25,11 @@ const createSdkWrapper = <P extends Parameters<typeof createSdk>[0]>(
* and we are creating a temp instance in order to export the getSessionToken
* even before the SDK was init
*/
sdkInstance = createSdkWrapper({ projectId: 'temp pid' });
globalSdk = createSdkWrapper({ projectId: 'temp pid' });

export const getSessionToken = () => {
if (IS_BROWSER) {
return sdkInstance?.getSessionToken();
return globalSdk?.getSessionToken();
}

// eslint-disable-next-line no-console
Expand All @@ -39,7 +39,7 @@ export const getSessionToken = () => {

export const getRefreshToken = () => {
if (IS_BROWSER) {
return sdkInstance?.getRefreshToken();
return globalSdk?.getRefreshToken();
}
// eslint-disable-next-line no-console
console.warn('Get refresh token is not supported in SSR');
Expand All @@ -48,15 +48,16 @@ export const getRefreshToken = () => {

export const getJwtPermissions = wrapInTry(
(token = getSessionToken(), tenant?: string) =>
sdkInstance?.getJwtPermissions(token, tenant)
globalSdk?.getJwtPermissions(token, tenant)
);

export const getJwtRoles = wrapInTry(
(token = getSessionToken(), tenant?: string) =>
sdkInstance?.getJwtRoles(token, tenant)
globalSdk?.getJwtRoles(token, tenant)
);

export const refresh = (token = getRefreshToken()) =>
sdkInstance?.refresh(token);
export const refresh = (token = getRefreshToken()) => globalSdk?.refresh(token);

export const getGlobalSdk = () => globalSdk;

export default createSdkWrapper;
18 changes: 18 additions & 0 deletions tests/Descope.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { shallowMount, mount } from '@vue/test-utils';
import Descope from '../src/Descope.vue';
import { default as DescopeWC } from '@descope/web-component';

jest.mock('../src/hooks', () => ({
useOptions: () => ({ projectId: 'project1', baseUrl: 'baseUrl' }),
Expand Down Expand Up @@ -68,6 +69,23 @@ describe('Descope.vue', () => {
expect(wrapper.vm.client).toBeNull();
});

it('init sdk config', async () => {
const wrapper = mount(Descope);
const descopeWc = wrapper.find('descope-wc');
expect(descopeWc).toBeTruthy();

expect(DescopeWC.sdkConfigOverrides).toEqual({
baseHeaders: {
'x-descope-sdk-name': 'vue',
'x-descope-sdk-version': '123'
},
persistTokens: false,
hooks: {
beforeRequest: expect.any(Function)
}
});
});

it('emits a success event when the DescopeWc component emits a success event', async () => {
const wrapper = mount(Descope);
const descopeWc = wrapper.find('descope-wc');
Expand Down

0 comments on commit d7dcfa5

Please sign in to comment.