Files
windmill/cli/auth.ts
T
Alexander Petric 8e87d412ac fix: fix circular dependancy breaking bundling of cli (#6219)
* fix: fix circular dependancy breaking bundling of cli

* remove hubpaths
2025-07-17 22:13:55 +00:00

62 lines
2.0 KiB
TypeScript

// deno-lint-ignore-file no-explicit-any
import { colors, log, setClient } from "./deps.ts";
import * as wmill from "./gen/services.gen.ts";
import { GlobalUserInfo } from "./gen/types.gen.ts";
import { loginInteractive, tryGetLoginInfo } from "./login.ts";
import { GlobalOptions } from "./types.ts";
import {
addWorkspace,
removeWorkspace,
Workspace
} from "./workspace.ts";
/**
* Main authentication function - moved from context.ts to break circular dependencies
* This function maintains the original API signature from context.ts
*/
export async function requireLogin(
opts: GlobalOptions
): Promise<GlobalUserInfo> {
// Import resolveWorkspace to avoid circular dependency at module level
const { resolveWorkspace } = await import("./context.ts");
const workspace = await resolveWorkspace(opts);
let token = await tryGetLoginInfo(opts);
if (!token) {
token = workspace.token;
}
setClient(token, workspace.remote.substring(0, workspace.remote.length - 1));
try {
return await wmill.globalWhoami();
} catch (error) {
// Check for network errors and provide clearer messages
const errorMsg = error instanceof Error ? error.message : String(error);
if (errorMsg.includes('fetch') || errorMsg.includes('connection') || errorMsg.includes('ECONNREFUSED') || errorMsg.includes('refused')) {
throw new Error(`Network error: Could not connect to Windmill server at ${workspace.remote}`);
}
log.info(
"! Could not reach API given existing credentials. Attempting to reauth..."
);
const newToken = await loginInteractive(workspace.remote);
if (!newToken) {
throw new Error("Unauthorized: Could not authenticate with the provided credentials");
}
// Update workspace token
removeWorkspace(workspace.name, false, opts);
workspace.token = newToken;
addWorkspace(workspace, opts);
setClient(
newToken,
workspace.remote.substring(0, workspace.remote.length - 1)
);
return await wmill.globalWhoami();
}
}