make maxAttempts configurable

This commit is contained in:
TheCactusBlue 2025-04-09 09:33:37 -07:00
parent 5b6b50f49d
commit b3576a29f0
3 changed files with 18 additions and 14 deletions

View File

@ -45,6 +45,7 @@
],
"dependencies": {
"@hookform/resolvers": "^3.3.4",
"@oslojs/otp": "^1.1.0",
"@simplewebauthn/browser": "^11.0.0",
"@stackframe/stack-sc": "workspace:*",
"@stackframe/stack-shared": "workspace:*",
@ -57,7 +58,7 @@
"js-cookie": "^3.0.5",
"lucide-react": "^0.378.0",
"oauth4webapi": "^2.10.3",
"@oslojs/otp": "^1.1.0",
"open": "^10.1.0",
"qrcode": "^1.5.4",
"react-avatar-editor": "^13.0.2",
"react-hook-form": "^7.51.4",

View File

@ -1592,7 +1592,7 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
* @param options.expiresInMillis Optional duration in milliseconds before the auth attempt expires (default: 2 hours)
* @returns Result containing either the refresh token or an error
*/
async promptCliLogin(options: { appUrl: string, expiresInMillis?: number }): Promise<Result<string, KnownErrors["CliAuthError"] | KnownErrors["CliAuthExpiredError"] | KnownErrors["CliAuthUsedError"]>> {
async promptCliLogin(options: { appUrl: string, expiresInMillis?: number, maxAttempts?: number, waitTimeMillis?: number }): Promise<Result<string, KnownErrors["CliAuthError"] | KnownErrors["CliAuthExpiredError"] | KnownErrors["CliAuthUsedError"]>> {
// Step 1: Initiate the CLI auth process
const response = await this._interface.sendClientRequest(
"/auth/cli",
@ -1627,9 +1627,7 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
// Step 3: Poll for the token
let attempts = 0;
const maxAttempts = 300; // 10 minutes with 2-second intervals
while (attempts < maxAttempts) {
while (attempts < (options.maxAttempts ?? Infinity)) {
attempts++;
const pollResponse = await this._interface.sendClientRequest("/auth/cli/poll", {
method: "POST",
@ -1645,16 +1643,18 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
if (pollResponse.status === 201 && pollResult.status === "success") {
return Result.ok(pollResult.refresh_token);
} else if (pollResult.status === "waiting") {
// Wait for 2 seconds before polling again
await wait(2000);
} else if (pollResult.status === "expired") {
return Result.error(new KnownErrors.CliAuthExpiredError("CLI authentication request expired. Please try again."));
} else if (pollResult.status === "used") {
return Result.error(new KnownErrors.CliAuthUsedError("This authentication token has already been used."));
} else {
return Result.error(new KnownErrors.CliAuthError(`Unexpected status from CLI auth polling: ${pollResult.status}`));
}
if (pollResult.status === "waiting") {
await wait(options.waitTimeMillis ?? 2000);
continue;
}
if (pollResult.status === "expired") {
return Result.error(new KnownErrors.CliAuthExpiredError("CLI authentication request expired. Please try again."));
}
if (pollResult.status === "used") {
return Result.error(new KnownErrors.CliAuthUsedError("This authentication token has already been used."));
}
return Result.error(new KnownErrors.CliAuthError(`Unexpected status from CLI auth polling: ${pollResult.status}`));
}
return Result.error(new KnownErrors.CliAuthError("Timed out waiting for CLI authentication."));

View File

@ -1551,6 +1551,9 @@ importers:
oauth4webapi:
specifier: ^2.10.3
version: 2.10.4
open:
specifier: ^10.1.0
version: 10.1.0
qrcode:
specifier: ^1.5.4
version: 1.5.4