Skip to content

Commit

Permalink
feat:add moment type support
Browse files Browse the repository at this point in the history
  • Loading branch information
gaokevin1 committed Dec 16, 2024
1 parent 85b3236 commit ca4318c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/sdks/web-js-sdk/src/sdk/fedcm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ type OneTapInitialize = ({

type PromptNotification = {
isSkippedMoment: () => boolean;
isDismissedMoment: () => boolean;
getMomentType: () => string;
};

/**
Expand All @@ -113,6 +115,7 @@ const createFedCM = (sdk: CoreSdk, projectId: string) => ({
oneTapConfig?: OneTapConfig,
loginOptions?: LoginOptions,
onSkip?: () => void,
onDismissed?: () => void,
) {
const readyProvider = provider ?? 'google';
const startResponse = await sdk.oauth.startNative(
Expand Down Expand Up @@ -150,8 +153,26 @@ const createFedCM = (sdk: CoreSdk, projectId: string) => ({
});

googleClient.prompt((notification) => {
if (notification?.isSkippedMoment()) {
const momentType = notification?.getMomentType();

if (!momentType) {
// Default to skipping behavior if no notification is provided
onSkip?.();
return;
}

switch (momentType) {
case 'skipped':
onSkip?.();
break;
case 'dismissed':
onDismissed?.();
break;
case 'display':
break;
default:
reportError(`Unexpected One Tap moment type: ${momentType}`);
break;
}
});
});
Expand Down
70 changes: 70 additions & 0 deletions packages/sdks/web-js-sdk/test/fedcm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,76 @@ describe('fedcm', () => {
expect(onSkip).toHaveBeenCalled();
});
});
describe('momentType handling', () => {
it('handles skipped moment correctly', async () => {
coreJs.oauth.startNative.mockResolvedValue({
ok: true,
data: { clientId: 'C123', stateId: 'S123', nonce: 'N123' },
});

const onSkip = jest.fn();

sdk.fedcm.oneTap(
'google',
{ auto_select: true },
{ stepup: false },
onSkip,
);
await new Promise(process.nextTick);

const promptCallback = googleClient.prompt.mock.calls[0][0];
promptCallback({ getMomentType: () => 'skipped', isSkippedMoment: () => true });

expect(onSkip).toHaveBeenCalled();
});

it('handles dismissed moment correctly', async () => {
coreJs.oauth.startNative.mockResolvedValue({
ok: true,
data: { clientId: 'C123', stateId: 'S123', nonce: 'N123' },
});

const onDismissed = jest.fn();

sdk.fedcm.oneTap(
'google',
{ auto_select: true },
{ stepup: false },
undefined,
onDismissed,
);
await new Promise(process.nextTick);

const promptCallback = googleClient.prompt.mock.calls[0][0];
promptCallback({ getMomentType: () => 'dismissed', isDismissedMoment: () => true });

expect(onDismissed).toHaveBeenCalled();
});

it('handles display moment correctly', async () => {
coreJs.oauth.startNative.mockResolvedValue({
ok: true,
data: { clientId: 'C123', stateId: 'S123', nonce: 'N123' },
});

const onDisplay = jest.fn();

sdk.fedcm.oneTap(
'google',
{ auto_select: true },
{ stepup: false },
undefined,
undefined,
);
await new Promise(process.nextTick);

const promptCallback = googleClient.prompt.mock.calls[0][0];
promptCallback({ getMomentType: () => 'display' });

// Verify no callbacks are called for 'display'
expect(onDisplay).not.toHaveBeenCalled()
});
});
describe('launch', () => {
it('should call navigator.credentials.get with correct parameters', async () => {
const mockGet = jest.fn();
Expand Down

0 comments on commit ca4318c

Please sign in to comment.