Skip to content

Commit

Permalink
packages/js: Automatically cancel any pending requests
Browse files Browse the repository at this point in the history
  • Loading branch information
merlindru committed Feb 13, 2024
1 parent 0cf812c commit cc3ee69
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/js/passkeys-next-auth-provider/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ interface ClientFirstLoginConfig extends Common {
tenantId: string;
}

/**
* As there can only be one ongoing passkey request at a time, this AbortController should
* be called to cancel the current request before starting a new one.
*
* After starting a new request, set this variable to the request's AbortController.
*/
let controller: AbortController | undefined;

/**
* Sign in with a passkey. Requires `PasskeyProvider` to be configured in `pages/api/auth/[...nextauth].ts`
*/
Expand All @@ -49,7 +57,7 @@ signInWithPasskey.conditional = function (config: SignInConfig, signal?: AbortSi
return noop;
}

let controller: AbortController | undefined;
controller?.abort(); // Abort any ongoing request
if (!signal) {
controller = new AbortController();
signal = controller.signal;
Expand All @@ -61,7 +69,7 @@ signInWithPasskey.conditional = function (config: SignInConfig, signal?: AbortSi
signal,
});

return (reason = "Manually aborted") => controller?.abort();
return (reason = "Manually aborted") => controller?.abort(reason);
};

/**
Expand All @@ -84,8 +92,12 @@ export async function clientFirstPasskeyLogin(config: ClientFirstLoginConfig): P
loginOptions.mediation = config.mediation;
}

controller?.abort(); // Abort any ongoing request
if (config.signal) {
loginOptions.signal = config.signal;
} else {
controller = new AbortController();
loginOptions.signal = controller.signal;
}

// Open "select passkey" dialog
Expand Down

0 comments on commit cc3ee69

Please sign in to comment.