Skip to content

Commit 63c8611

Browse files
authored
Merge pull request #1614 from appwrite/fix/cli-cloud-push-oauth-regions
Fix CLI cloud push flows
2 parents 3dbb283 + 1b005cf commit 63c8611

7 files changed

Lines changed: 393 additions & 126 deletions

File tree

templates/cli/lib/auth/login.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import inquirer from "inquirer";
22
import { Account, type Models } from "@appwrite.io/console";
3-
import { sdkForConsole } from "../sdks.js";
3+
import { getValidAccessToken, sdkForConsole } from "../sdks.js";
44
import { globalConfig, normalizeCloudConsoleEndpoint } from "../config.js";
55
import {
66
EXECUTABLE_NAME,
@@ -138,6 +138,18 @@ export const getCurrentAccount = async (): Promise<Models.User | null> => {
138138
return account;
139139
} catch (err) {
140140
if (isGuestUnauthorizedError(err)) {
141+
try {
142+
await getValidAccessToken(globalConfig.getEndpoint(), {
143+
forceRefresh: true,
144+
});
145+
const refreshedClient = await sdkForConsole();
146+
const refreshedAccount = await new Account(refreshedClient).get();
147+
globalConfig.setEmail(refreshedAccount.email);
148+
return refreshedAccount;
149+
} catch (_refreshError) {
150+
// Fall through to local cleanup only after refresh recovery fails.
151+
}
152+
141153
removeCurrentSession();
142154
}
143155
return null;

templates/cli/lib/commands/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ const SettingsSchema = z
120120
security: z
121121
.object({
122122
duration: z.union([z.number(), z.bigint()]).optional(),
123-
limit: z.union([z.number(), z.bigint()]).optional(),
124-
sessionsLimit: z.union([z.number(), z.bigint()]).optional(),
125-
passwordHistory: z.union([z.number(), z.bigint()]).optional(),
123+
limit: z.union([z.number(), z.bigint()]).nullable().optional(),
124+
sessionsLimit: z.union([z.number(), z.bigint()]).nullable().optional(),
125+
passwordHistory: z.union([z.number(), z.bigint()]).nullable().optional(),
126126
passwordDictionary: z.boolean().optional(),
127127
personalDataCheck: z.boolean().optional(),
128128
sessionAlerts: z.boolean().optional(),

templates/cli/lib/commands/init.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ const getExistingProjectSummary = async (
123123
};
124124
};
125125

126+
const getRegionalCloudEndpoint = (region: string): string => {
127+
const url = new URL(globalConfig.getEndpoint() || DEFAULT_ENDPOINT);
128+
url.hostname = `${region}.${url.hostname}`;
129+
return url.toString().replace(/\/$/, "");
130+
};
131+
126132
const printInitProjectSuccess = (message: string): void => {
127133
console.log(`${chalk.green.bold("✓")} ${chalk.green(message)}`);
128134
};
@@ -359,7 +365,6 @@ const initProject = async ({
359365
}
360366

361367
localConfig.clear(); // Clear the config to avoid any conflicts
362-
const url = new URL(DEFAULT_ENDPOINT);
363368

364369
if (answers.start === "new") {
365370
let projectIdToCreate;
@@ -394,9 +399,7 @@ const initProject = async ({
394399
localConfig.setProject(response["$id"], response.name ?? "");
395400
localConfig.setOrganizationId(answers.organization);
396401
if (answers.region) {
397-
localConfig.setEndpoint(
398-
`https://${answers.region}.${url.host}${url.pathname}`,
399-
);
402+
localConfig.setEndpoint(getRegionalCloudEndpoint(answers.region));
400403
}
401404
} else {
402405
let selectedProject;
@@ -417,9 +420,7 @@ const initProject = async ({
417420
localConfig.setOrganizationId(answers.organization);
418421

419422
if (isCloud() && selectedProject.region) {
420-
localConfig.setEndpoint(
421-
`https://${selectedProject.region}.${url.host}${url.pathname}`,
422-
);
423+
localConfig.setEndpoint(getRegionalCloudEndpoint(selectedProject.region));
423424
}
424425
}
425426

0 commit comments

Comments
 (0)