From 53c984741b294ed8aad9ce04b4c428637e67cb25 Mon Sep 17 00:00:00 2001 From: TheCactusBlue Date: Fri, 4 Apr 2025 11:45:52 -0700 Subject: [PATCH] change to results --- packages/stack-shared/src/known-errors.tsx | 33 +++++++++++++++++++ .../apps/implementations/client-app-impl.ts | 18 +++++----- .../stack-app/apps/interfaces/client-app.ts | 2 +- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/packages/stack-shared/src/known-errors.tsx b/packages/stack-shared/src/known-errors.tsx index eed80eaed..48b00feec 100644 --- a/packages/stack-shared/src/known-errors.tsx +++ b/packages/stack-shared/src/known-errors.tsx @@ -1218,6 +1218,36 @@ const InvalidPollingCodeError = createKnownErrorConstructor( (json: any) => [json] as const, ); +const CliAuthError = createKnownErrorConstructor( + KnownError, + "CLI_AUTH_ERROR", + (message: string) => [ + 400, + message, + ] as const, + (json: any) => [json.message] as const, +); + +const CliAuthExpiredError = createKnownErrorConstructor( + KnownError, + "CLI_AUTH_EXPIRED_ERROR", + (message: string = "CLI authentication request expired. Please try again.") => [ + 400, + message, + ] as const, + (json: any) => [json.message] as const, +); + +const CliAuthUsedError = createKnownErrorConstructor( + KnownError, + "CLI_AUTH_USED_ERROR", + (message: string = "This authentication token has already been used.") => [ + 400, + message, + ] as const, + (json: any) => [json.message] as const, +); + const PermissionIdAlreadyExists = createKnownErrorConstructor( KnownError, "PERMISSION_ID_ALREADY_EXISTS", @@ -1243,6 +1273,9 @@ export const KnownErrors = { AllOverloadsFailed, ProjectAuthenticationError, PermissionIdAlreadyExists, + CliAuthError, + CliAuthExpiredError, + CliAuthUsedError, InvalidProjectAuthentication, ProjectKeyWithoutAccessType, InvalidAccessType, diff --git a/packages/template/src/lib/stack-app/apps/implementations/client-app-impl.ts b/packages/template/src/lib/stack-app/apps/implementations/client-app-impl.ts index f924336c8..b89d39ba6 100644 --- a/packages/template/src/lib/stack-app/apps/implementations/client-app-impl.ts +++ b/packages/template/src/lib/stack-app/apps/implementations/client-app-impl.ts @@ -1476,11 +1476,11 @@ export class _StackClientAppImplIncomplete { + async promptCliLogin(options: { appUrl: string, expiresInMillis?: number }): Promise> { if (!options.appUrl) { - throw new Error("appUrl is required and must be set to the URL of the app you're authenticating with"); + return Result.error(new KnownErrors.CliAuthError("appUrl is required and must be set to the URL of the app you're authenticating with")); } // Step 1: Initiate the CLI auth process @@ -1499,7 +1499,7 @@ export class _StackClientAppImplIncomplete> { diff --git a/packages/template/src/lib/stack-app/apps/interfaces/client-app.ts b/packages/template/src/lib/stack-app/apps/interfaces/client-app.ts index f5ac2673f..544ab6065 100644 --- a/packages/template/src/lib/stack-app/apps/interfaces/client-app.ts +++ b/packages/template/src/lib/stack-app/apps/interfaces/client-app.ts @@ -42,7 +42,7 @@ export type StackClientApp>, signInWithPasskey(): Promise>, callOAuthCallback(): Promise, - promptCliLogin(options: { appUrl: string, expiresInMillis?: number }): Promise, + promptCliLogin(options: { appUrl: string, expiresInMillis?: number }): Promise>, sendForgotPasswordEmail(email: string, options?: { callbackUrl?: string }): Promise>, sendMagicLinkEmail(email: string, options?: { callbackUrl?: string }): Promise>, resetPassword(options: { code: string, password: string }): Promise>,