Skip to content

Commit 37ce578

Browse files
committed
Fix CLI OAuth review comments
1 parent 9db7e4c commit 37ce578

5 files changed

Lines changed: 21 additions & 3 deletions

File tree

templates/cli/lib/auth/login.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,6 @@ export const loginCommand = async ({
382382
switch?: boolean;
383383
new?: boolean;
384384
}): Promise<void> => {
385-
let oldCurrent = globalConfig.getCurrentSession();
386-
387385
if (switchAccount && newAccount) {
388386
throw new Error("Use either --switch or --new, not both.");
389387
}
@@ -400,7 +398,7 @@ export const loginCommand = async ({
400398
const shouldUseCloudLogin =
401399
isFlagEnabled("oauthLogin") && isCloudLoginEndpoint(configEndpoint);
402400

403-
oldCurrent = globalConfig.getCurrentSession();
401+
let oldCurrent = globalConfig.getCurrentSession();
404402

405403
if (oldCurrent !== "" && !newAccount) {
406404
let account: Models.User | null = null;
@@ -409,6 +407,7 @@ export const loginCommand = async ({
409407
} catch (_err) {
410408
account = null;
411409
}
410+
// Refresh the current session after account lookup.
412411
oldCurrent = globalConfig.getCurrentSession();
413412

414413
if (account) {

templates/cli/lib/commands/services/services.ts.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ const {{ commandVar }} = {{ service.name | caseCamel }}
160160
async ({{ actionParams }}) => {
161161
const url = await (await get{{ service.name | caseUcfirst }}Client()).{{ method.name | caseCamel }}({{ argExpressions | join(', ') | raw }});
162162
const response = await fetch(url);
163+
if (!response.ok) {
164+
throw new Error(`Failed to download file: ${response.status} ${response.statusText}`);
165+
}
163166
const buffer = Buffer.from(await response.arrayBuffer());
164167
fs.writeFileSync(destination, buffer);
165168
success(`File saved to ${destination}`);

templates/cli/lib/sdks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export const getValidAccessToken = async (
2828
return accessToken;
2929
}
3030

31+
if (accessToken && tokenExpiry === 0) {
32+
return accessToken;
33+
}
34+
3135
if (!refreshToken) {
3236
throw new Error(
3337
`Session expired. Please run \`${EXECUTABLE_NAME} login\` to create a new session.`,

tests/e2e/Base.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ abstract class Base extends TestCase
265265
'auth:poll-device-token-empty-error:passed',
266266
'auth:poll-device-token-default-interval:passed',
267267
'auth:valid-access-token-cached:passed',
268+
'auth:valid-access-token-missing-expiry:passed',
268269
'auth:oauth-login-flag:passed',
269270
];
270271

tests/e2e/languages/cli/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,17 @@ async function runAuthChecks() {
10081008
assert.equal(token, "cached-token");
10091009
});
10101010

1011+
await authCheck("valid-access-token-missing-expiry", async () => {
1012+
globalConfig.clear();
1013+
globalConfig.addSession("tok2", {
1014+
endpoint: "http://localhost/v1",
1015+
accessToken: "cached-token-without-expiry",
1016+
});
1017+
globalConfig.setCurrentSession("tok2");
1018+
const token = await getValidAccessToken("http://localhost/v1");
1019+
assert.equal(token, "cached-token-without-expiry");
1020+
});
1021+
10111022
await authCheck("oauth-login-flag", () => {
10121023
const prev = process.env.APPWRITE_CLI_OAUTH_LOGIN;
10131024
delete process.env.APPWRITE_CLI_OAUTH_LOGIN;

0 commit comments

Comments
 (0)