()
| 119 | } |
| 120 | |
| 121 | private async setApiClientAndCurrentUser() { |
| 122 | let apiKey = await this.effects.getObservableApiKey(this.effects); |
| 123 | const apiClient = new ObservableApiClient( |
| 124 | apiKey ? {apiKey, clack: this.effects.clack} : {clack: this.effects.clack} |
| 125 | ); |
| 126 | |
| 127 | let currentUser: GetCurrentUserResponse | null = null; |
| 128 | let authError: null | "unauthenticated" | "forbidden" = null; |
| 129 | try { |
| 130 | if (apiKey) { |
| 131 | currentUser = await apiClient.getCurrentUser(); |
| 132 | // List of valid workspaces that can be used to create projects. |
| 133 | currentUser = {...currentUser, workspaces: validWorkspaces(currentUser.workspaces)}; |
| 134 | } |
| 135 | } catch (error) { |
| 136 | if (isHttpError(error)) { |
| 137 | if (error.statusCode === 401) authError = "unauthenticated"; |
| 138 | else if (error.statusCode === 403) authError = "forbidden"; |
| 139 | else throw error; |
| 140 | } else { |
| 141 | throw error; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (!currentUser) { |
| 146 | if (!this.effects.isTty) { |
| 147 | if (authError === "unauthenticated" || !apiKey) { |
| 148 | throw new CliError("No authentication provided"); |
| 149 | } else { |
| 150 | const source = |
| 151 | apiKey.source == "file" |
| 152 | ? ` from ${apiKey.filePath}` |
| 153 | : apiKey.source === "env" |
| 154 | ? ` from $${apiKey.envVar}` |
| 155 | : ""; |
| 156 | throw new CliError(`Authentication${source} was rejected by the server: ${authError ?? "unknown error"}`); |
| 157 | } |
| 158 | } |
| 159 | const message = |
| 160 | authError === "unauthenticated" || authError === null |
| 161 | ? "You must be logged in to Observable to deploy. Do you want to do that now?" |
| 162 | : "Your authentication is invalid. Do you want to log in to Observable again?"; |
| 163 | const choice = await this.effects.clack.confirm({ |
| 164 | message, |
| 165 | active: "Yes, log in", |
| 166 | inactive: "No, cancel deploy" |
| 167 | }); |
| 168 | if (!choice) { |
| 169 | this.effects.clack.outro(yellow("Deploy canceled.")); |
| 170 | } |
| 171 | if (this.effects.clack.isCancel(choice) || !choice) |
| 172 | throw new CliError("User canceled deploy", {print: false, exitCode: 0}); |
| 173 | |
| 174 | ({currentUser, apiKey} = await loginInner(this.effects, {pollTime: this.deployOptions.deployPollInterval})); |
| 175 | apiClient.setApiKey(apiKey); |
| 176 | } |
| 177 | |
| 178 | if (!currentUser) throw new CliError(commandRequiresAuthenticationMessage); |
no test coverage detected