Skip to content
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

feat(cli): headless login #1327

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 10 additions & 5 deletions apps/cli/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ export const getCLIAccessToken = async (
): Promise<GetScopedTokenResponse> => {
const url = new URL(`${process.env.AUTH_BACKEND_URL}/appToken`);

const res = await Axios.get<GetScopedTokenResponse>(url.toString(), {
headers: { Authorization: `Bearer ${accessToken}` },
timeout: 10000,
});
try {
const res = await Axios.get<GetScopedTokenResponse>(url.toString(), {
headers: { Authorization: `Bearer ${accessToken}` },
timeout: 10000,
});

return res.data;
return res.data;
} catch (err) {
console.log(err);
return { token: "" };
}
};

export const validateCLIToken = async (
Expand Down
14 changes: 13 additions & 1 deletion apps/cli/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ const routeUserToStudioForLogin = (
};
export const handleLoginCliCommand = async (options: {
printer: Printer;
token?: string;
}) => {
const { printer } = options;
const { printer, token } = options;

const userData = await getCurrentUserData();
if (userData !== null) {
Expand All @@ -46,6 +47,17 @@ export const handleLoginCliCommand = async (options: {
return;
}

if (token) {
const { token: cliToken } = await getCLIAccessToken(token);
await credentialsStorage.set(CredentialsStorageType.ACCOUNT, cliToken);

printer.printConsoleMessage(
"info",
chalk.bold.cyan("You are successfully logged in."),
);
return;
}

const { id: sessionId, iv: initVector } = await generateUserLoginIntent();

const spinner = printer.withLoaderMessage(
Expand Down
12 changes: 10 additions & 2 deletions apps/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,20 @@ export const main = async () => {
.command(
"login",
"logs in through authentication in the Codemod Studio",
(y) => y,
(y) =>
y.option("token", {
alias: "t",
type: "string",
description: "clerk token for headless login",
hidden: true,
}),
async (args) => {
const { executeCliCommand, printer } =
await initializeDependencies(args);

return executeCliCommand(() => handleLoginCliCommand({ printer }));
return executeCliCommand(() =>
handleLoginCliCommand({ printer, token: args.token }),
);
},
)
.command(
Expand Down
Loading