Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return sdk script results after conditional UI #878

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/sdks/web-component/src/lib/descope-wc/DescopeWc.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure I understand the problem and how this change solves it
Can you please elaborate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem is that after biometrics auto-fill (which trigger this next(...) call) - sdk scripts (such as forter/fingerprint) don't work

the reason that they don't work is because we don't send their state like on other "regular" next calls

@nirgur if you prefer we can talk

Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ class DescopeWc extends BaseDescopeWc {
flowConfig.version,
projectConfig.componentsVersion,
{
...this.getComponentsContext(),
transactionId: webauthnTransactionId,
response,
failure,
Expand Down Expand Up @@ -937,6 +938,7 @@ class DescopeWc extends BaseDescopeWc {
flowConfig.version,
projectConfig.componentsVersion,
{
...this.getComponentsContext(),
transactionId,
response,
},
Expand Down
43 changes: 40 additions & 3 deletions packages/sdks/web-component/test/webauthnConditionalUi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { fireEvent, waitFor } from '@testing-library/dom';
import { screen } from 'shadow-dom-testing-library';
import { generateSdkResponse, invokeScriptOnload } from './testUtils';
import '../src/lib/descope-wc';

import { isConditionalLoginSupported } from '../src/lib/helpers/webauthn';
import { RESPONSE_ACTIONS } from '../src/lib/constants';
import { RESPONSE_ACTIONS, SDK_SCRIPT_RESULTS_KEY } from '../src/lib/constants';
// We load forter script in the test because we mock it and ensure it is called properly
import loadForter from '../src/lib/descope-wc/sdkScripts/forter';

jest.mock('../src/lib/descope-wc/sdkScripts/forter', () => jest.fn());

jest.mock('../src/lib/helpers/webauthn', () => ({
isConditionalLoginSupported: jest.fn(),
Expand Down Expand Up @@ -48,7 +51,7 @@ globalThis.DescopeUI = {};
// this is for mocking the pages/theme/config
const themeContent = {};
let pageContent = '';
const configContent = {
let configContent: any = {
componentsVersion: '1.2.3',
};

Expand Down Expand Up @@ -252,6 +255,29 @@ describe('webauthnConditionalUi', () => {
});

it('should call next with the correct params when user is logging in using autofill', async () => {
// We use specific connector which exists to test it all end to end
// but we override it above
const scriptId = 'forter';
const resultKey = 'some-result-key';
const resultValue = 'some-value';

configContent = {
componentsVersion: '1.2.3',
flows: {
otpSignInEmail: {
sdkScripts: [
{
id: scriptId,
initArgs: {
siteId: 'some-site-id',
},
resultKey,
},
],
},
},
};

startMock.mockReturnValue(generateSdkResponse());
isConditionalLoginSupportedMock.mockReturnValueOnce(true);
isWebauthnSupportedMock.mockReturnValueOnce(true);
Expand All @@ -265,9 +291,20 @@ describe('webauthnConditionalUi', () => {

document.body.innerHTML = `<h1>Custom element test</h1> <descope-wc flow-id="otpSignInEmail" project-id="1"></descope-wc>`;

// wait for loadForter to be called
await waitFor(() => expect(loadForter).toHaveBeenCalled(), {
timeout: 3000,
});

// trigger the callback, to simulate the script loaded
// get the 3rd argument of the first call to loadForter
const callback = (loadForter as jest.Mock).mock.calls[0][2];
callback(resultValue);

await waitFor(
() =>
expect(nextMock).toHaveBeenCalledWith('0', '0', 'id', 0, '1.2.3', {
[`${SDK_SCRIPT_RESULTS_KEY}.${scriptId}_${resultKey}`]: resultValue, // should be called with the result of the script
response: 'response',
transactionId: 'transactionId',
}),
Expand Down
Loading