Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"commander": "^12.1.0",
"globby": "^16.1.0",
"jsonc-parser": "^3.3.1",
"ky": "^1.14.2",
"p-wait-for": "^6.0.0",
"zod": "^4.3.5"
},
Expand Down
41 changes: 29 additions & 12 deletions src/cli/commands/auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Command } from "commander";
import chalk from "chalk";
import { log } from "@clack/prompts";
import pWaitFor from "p-wait-for";
import {
writeAuth,
generateDeviceCode,
getTokenFromDeviceCode,
getUserInfo,
} from "@core/auth/index.js";
import type {
DeviceCodeResponse,
TokenResponse,
UserInfoResponse,
} from "@core/auth/index.js";
import type { DeviceCodeResponse, TokenResponse } from "@core/auth/index.js";
import { runCommand, runTask } from "../../utils/index.js";

async function generateAndDisplayDeviceCode(): Promise<DeviceCodeResponse> {
Expand All @@ -22,16 +28,17 @@ async function generateAndDisplayDeviceCode(): Promise<DeviceCodeResponse> {
);

log.info(
`Please visit: ${deviceCodeResponse.verificationUrl}\n` +
`Enter your device code: ${deviceCodeResponse.userCode}`
`Your code is: ${chalk.bold(deviceCodeResponse.userCode)}` +
`\nPlease visit: ${deviceCodeResponse.verificationUriComplete}`
);

return deviceCodeResponse;
}

async function waitForAuthentication(
deviceCode: string,
expiresIn: number
expiresIn: number,
interval: number
): Promise<TokenResponse> {
let tokenResponse: TokenResponse | undefined;

Expand All @@ -49,7 +56,7 @@ async function waitForAuthentication(
return false;
},
{
interval: 2000,
interval: interval * 1000,
timeout: expiresIn * 1000,
}
);
Expand All @@ -73,11 +80,18 @@ async function waitForAuthentication(
return tokenResponse;
}

async function saveAuthData(token: TokenResponse): Promise<void> {
async function saveAuthData(
response: TokenResponse,
userInfo: UserInfoResponse
): Promise<void> {
const expiresAt = Date.now() + response.expiresIn * 1000;

await writeAuth({
token: token.token,
email: token.email,
name: token.name,
accessToken: response.accessToken,
refreshToken: response.refreshToken,
expiresAt,
email: userInfo.email,
name: userInfo.name,
});
}

Expand All @@ -86,12 +100,15 @@ async function login(): Promise<void> {

const token = await waitForAuthentication(
deviceCodeResponse.deviceCode,
deviceCodeResponse.expiresIn
deviceCodeResponse.expiresIn,
deviceCodeResponse.interval
);

await saveAuthData(token);
const userInfo = await getUserInfo(token.accessToken);

await saveAuthData(token, userInfo);

log.success(`Logged in as ${token.name}`);
log.success(`Successfully logged in as ${chalk.bold(userInfo.email)}`);
}

export const loginCommand = new Command("login")
Expand Down
4 changes: 2 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env node

import { Command } from "commander";
import { getPackageVersion } from "./utils/index.js";
import { loginCommand } from "./commands/auth/login.js";
import { whoamiCommand } from "./commands/auth/whoami.js";
import { logoutCommand } from "./commands/auth/logout.js";
import { showProjectCommand } from "./commands/project/show-project.js";
import packageJson from "../../package.json";

const program = new Command();

Expand All @@ -14,7 +14,7 @@ program
.description(
"Base44 CLI - Unified interface for managing Base44 applications"
)
.version(getPackageVersion());
.version(packageJson.version);

// Register authentication commands
program.addCommand(loginCommand);
Expand Down
2 changes: 0 additions & 2 deletions src/cli/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export * from "./packageVersion.js";
export * from "./runCommand.js";
export * from "./runTask.js";

14 changes: 0 additions & 14 deletions src/cli/utils/packageVersion.ts

This file was deleted.

8 changes: 2 additions & 6 deletions src/cli/utils/runCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { intro, log } from "@clack/prompts";
import chalk from "chalk";
import { AuthApiError, AuthValidationError } from "@core/errors.js";

const base44Color = chalk.bgHex("#E86B3C");

Expand All @@ -18,11 +17,8 @@ export async function runCommand(
try {
await commandFn();
} catch (e) {
if (e instanceof AuthValidationError) {
const issues = e.issues.map((i) => i.message).join(", ");
log.error(`Invalid response from server: ${issues}`);
} else if (e instanceof AuthApiError || e instanceof Error) {
log.error(e.message);
if (e instanceof Error) {
log.error(e.stack ?? e.message);
} else {
log.error(String(e));
}
Expand Down
Loading