Skip to content

feat: automatic cloudflare login #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
27 changes: 27 additions & 0 deletions alchemy/bin/alchemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { createCli, trpcServer, zod as z } from "trpc-cli";
import { createAlchemy } from "./commands/create.ts";
import { runLogin, type LoginInput } from "./commands/login.ts";
import { getPackageVersion } from "./services/get-package-version.ts";
import {
EditorSchema,
Expand Down Expand Up @@ -59,6 +60,32 @@ const router = t.router({
};
await createAlchemy(combinedInput);
}),
login: t.procedure
.meta({
description: "Login to Cloudflare using OAuth",
})
.input(
z.tuple([
z
.object({
scopes: z
.array(z.string())
.optional()
.describe(
"Cloudflare OAuth scopes to request (defaults to all available scopes)",
),
})
.optional()
.default({}),
]),
)
.mutation(async ({ input }) => {
const [options] = input;
const loginInput: LoginInput = {
scopes: options.scopes,
};
await runLogin(loginInput);
}),
});

export type AppRouter = typeof router;
Expand Down
37 changes: 37 additions & 0 deletions alchemy/bin/commands/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { intro, log, outro } from "@clack/prompts";
import pc from "picocolors";
import { DefaultScopes, loginToCloudflare } from "../../src/cloudflare/auth.ts";

export interface LoginInput {
scopes?: string[];
}

export async function runLogin(input: LoginInput): Promise<void> {
try {
intro(pc.cyan("🔐 Cloudflare Login"));

const scopesToUse =
input.scopes && input.scopes.length > 0
? input.scopes
: Object.keys(DefaultScopes);

log.info(
`Requesting the following scopes:\n${scopesToUse.map((scope) => ` • ${scope}`).join("\n")}`,
);

await loginToCloudflare(scopesToUse);

outro(pc.green("✅ Login successful!"));
} catch (error) {
log.error("Login failed:");
if (error instanceof Error) {
log.error(`${pc.red("Error:")} ${error.message}`);
if (error.stack && process.env.DEBUG) {
log.error(`${pc.gray("Stack trace:")}\n${error.stack}`);
}
} else {
log.error(pc.red(String(error)));
}
process.exit(1);
}
}
1 change: 1 addition & 0 deletions alchemy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"jszip": "^3.0.0",
"kleur": "^4.1.5",
"libsodium-wrappers": "^0.7.15",
"open": "^10.1.2",
"turndown": "^7.0.0",
"unenv": "2.0.0-rc.15",
"yaml": "^2.0.0"
Expand Down
Loading
Loading