Skip to content

Commit b457c17

Browse files
authored
fix: Issue8683 (#561)
1 parent afcaf55 commit b457c17

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

lib/index.test.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,117 @@ describe('sdk', () => {
538538
}),
539539
);
540540
});
541+
542+
it('should add auth management key to request when there is no token', async () => {
543+
jest.resetModules();
544+
const createCoreJs = jest.fn();
545+
546+
jest.doMock('@descope/core-js-sdk', () => ({
547+
__esModule: true,
548+
default: createCoreJs,
549+
wrapWith: (sdkInstance: object) => sdkInstance,
550+
addHooksToConfig: (config, hooks) => {
551+
// eslint-disable-next-line no-param-reassign
552+
config.hooks = hooks;
553+
return config;
554+
},
555+
}));
556+
const createNodeSdk = require('.').default; // eslint-disable-line
557+
558+
createNodeSdk({
559+
projectId: 'project-id',
560+
authManagementKey: 'auth-management-key-123',
561+
});
562+
563+
expect(createCoreJs).toHaveBeenCalledWith(
564+
expect.objectContaining({
565+
hooks: expect.objectContaining({
566+
beforeRequest: expect.any(Array),
567+
}),
568+
}),
569+
);
570+
571+
// Get the hooks that were passed
572+
const config = createCoreJs.mock.calls[0][0];
573+
const beforeRequestHooks = config.hooks.beforeRequest;
574+
575+
// Test the first hook (our auth management key hook)
576+
const requestConfig = { url: 'test' };
577+
const result = beforeRequestHooks[0](requestConfig);
578+
579+
expect(result.token).toBe('auth-management-key-123');
580+
});
581+
it('should add auth management key to request when there is token', async () => {
582+
jest.resetModules();
583+
const createCoreJs = jest.fn();
584+
585+
jest.doMock('@descope/core-js-sdk', () => ({
586+
__esModule: true,
587+
default: createCoreJs,
588+
wrapWith: (sdkInstance: object) => sdkInstance,
589+
addHooksToConfig: (config, hooks) => {
590+
// eslint-disable-next-line no-param-reassign
591+
config.hooks = hooks;
592+
return config;
593+
},
594+
}));
595+
const createNodeSdk = require('.').default; // eslint-disable-line
596+
597+
createNodeSdk({
598+
projectId: 'project-id',
599+
authManagementKey: 'auth-management-key-123',
600+
});
601+
602+
// Get the hooks that were passed
603+
const config = createCoreJs.mock.calls[0][0];
604+
const beforeRequestHooks = config.hooks.beforeRequest;
605+
606+
// Test the first hook with existing token
607+
const requestConfig = { url: 'test', token: 'existing-token' };
608+
const result = beforeRequestHooks[0](requestConfig);
609+
610+
expect(result.token).toBe('existing-token:auth-management-key-123');
611+
});
612+
it('should merge before request hooks if they are defined', async () => {
613+
jest.resetModules();
614+
const createCoreJs = jest.fn();
615+
const existingHook = jest.fn((config) => ({ ...config, customField: 'test' }));
616+
617+
jest.doMock('@descope/core-js-sdk', () => ({
618+
__esModule: true,
619+
default: createCoreJs,
620+
wrapWith: (sdkInstance: object) => sdkInstance,
621+
addHooksToConfig: (config, hooks) => {
622+
// eslint-disable-next-line no-param-reassign
623+
config.hooks = hooks;
624+
return config;
625+
},
626+
}));
627+
const createNodeSdk = require('.').default; // eslint-disable-line
628+
629+
createNodeSdk({
630+
projectId: 'project-id',
631+
authManagementKey: 'auth-management-key-123',
632+
hooks: {
633+
beforeRequest: [existingHook],
634+
},
635+
});
636+
637+
const config = createCoreJs.mock.calls[0][0];
638+
const beforeRequestHooks = config.hooks.beforeRequest;
639+
640+
// Should have 2 hooks: our auth management hook + the existing hook
641+
expect(beforeRequestHooks).toHaveLength(2);
642+
643+
// Test that both hooks are executed
644+
const requestConfig = { url: 'test' };
645+
const afterFirstHook = beforeRequestHooks[0](requestConfig);
646+
const afterSecondHook = beforeRequestHooks[1](afterFirstHook);
647+
648+
expect(afterFirstHook.token).toBe('auth-management-key-123');
649+
expect(afterSecondHook.customField).toBe('test');
650+
expect(existingHook).toHaveBeenCalledWith(afterFirstHook);
651+
});
541652
});
542653

543654
describe('public key', () => {

lib/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ const nodeSdk = ({ authManagementKey, managementKey, publicKey, ...config }: Nod
5151
...config.hooks,
5252
beforeRequest: [
5353
(requestConfig) => {
54-
if (!requestConfig.token && authManagementKey) {
54+
if (authManagementKey) {
5555
// eslint-disable-next-line no-param-reassign
56-
requestConfig.token = authManagementKey;
56+
requestConfig.token = !requestConfig.token
57+
? authManagementKey
58+
: `${requestConfig.token}:${authManagementKey}`;
5759
}
5860

5961
return requestConfig;

0 commit comments

Comments
 (0)