Skip to content

Commit

Permalink
Add nativeResume function and have polling check the current action
Browse files Browse the repository at this point in the history
  • Loading branch information
itaihanski committed Nov 8, 2024
1 parent 82e2fa7 commit b656205
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions packages/sdks/web-component/src/lib/descope-wc/DescopeWc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
FETCH_ERROR_RESPONSE_ERROR_CODE,
FETCH_EXCEPTION_ERROR_CODE,
RESPONSE_ACTIONS,
URL_CODE_PARAM_NAME,
URL_RUN_IDS_PARAM_NAME,
URL_TOKEN_PARAM_NAME,
} from '../constants';
import {
fetchContent,
Expand Down Expand Up @@ -106,6 +109,44 @@ class DescopeWc extends BaseDescopeWc {
// the native layer as a response to a dispatched 'bridge' event.
nativeComplete: (bridgeResponse: string) => Promise<void>;

// This callback is called by the `nativeBridge` when the app
// is redirected back to using a deep link or equivalent mechanism.
// It is currently supported to handle magic links and OAuth / SSO
nativeResume = ({
urlString,
code,
}: {
urlString: string;
code?: string;
}) => {
const url = new URL(urlString);
const token = url.searchParams.get(URL_TOKEN_PARAM_NAME);
const exchangeCode = code || url.searchParams.get(URL_CODE_PARAM_NAME);
const stepId = url.searchParams
.get(URL_RUN_IDS_PARAM_NAME)
.split('_')
.pop();

if (token && stepId) {
this.logger.info('nativeResume received a magic link - updating state');
this.#resetPollingTimeout();
// update the state along with cancelling out the action to abort the polling mechanism
this.flowState.update({ token, stepId, action: undefined });
} else if (exchangeCode) {
this.logger.info(
'nativeResume received an exchange code - calling native complete',
);
this.nativeComplete(
JSON.stringify({
exchangeCode,
idpInitiated: true,
}),
);
} else {
this.logger.warn('nativeResume called with unsupported url');
}
};

// This object is set by the native layer to
// inject native specific data into the 'flowState'.
nativeOptions:
Expand Down Expand Up @@ -512,7 +553,6 @@ class DescopeWc extends BaseDescopeWc {
this.#handlePollingResponse(
executionId,
stepId,
action,
flowConfig.version,
projectConfig.componentsVersion,
);
Expand Down Expand Up @@ -622,7 +662,6 @@ class DescopeWc extends BaseDescopeWc {
#handlePollingResponse = (
executionId: string,
stepId: string,
action: string,
flowVersion: number,
componentsVersion: string,
rescheduled: boolean = false,
Expand All @@ -632,7 +671,7 @@ class DescopeWc extends BaseDescopeWc {
const pollingThrottleDelay = 500;
const pollingThrottleThreshold = 500;
const pollingThrottleTimeout = 1000;
if (action === RESPONSE_ACTIONS.poll) {
if (this.flowState.current.action === RESPONSE_ACTIONS.poll) {
// schedule next polling request for 2 seconds from now
this.logger.debug('polling - Scheduling polling request');
const scheduledAt = Date.now();
Expand Down Expand Up @@ -676,7 +715,6 @@ class DescopeWc extends BaseDescopeWc {
this.#handlePollingResponse(
executionId,
stepId,
action,
flowVersion,
componentsVersion,
throttled,
Expand All @@ -691,7 +729,6 @@ class DescopeWc extends BaseDescopeWc {
this.#handlePollingResponse(
executionId,
stepId,
action,
flowVersion,
componentsVersion,
);
Expand All @@ -707,12 +744,10 @@ class DescopeWc extends BaseDescopeWc {
}

this.#handleSdkResponse(sdkResp);
const { action: nextAction } = sdkResp?.data ?? {};
// will poll again if needed
this.#handlePollingResponse(
executionId,
stepId,
nextAction,
flowVersion,
componentsVersion,
);
Expand Down

0 comments on commit b656205

Please sign in to comment.