Skip to content

v3: fix for fresh CLI logins after CLI token revoke #1056

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

Merged
merged 5 commits into from
Apr 24, 2024
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
5 changes: 5 additions & 0 deletions .changeset/rich-kangaroos-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---

Fix a bug where revoking the CLI token would prevent you from ever logging in again with the CLI.
27 changes: 23 additions & 4 deletions apps/webapp/app/services/personalAccessToken.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,27 @@ export async function authenticatePersonalAccessToken(

const hashedToken = hashToken(token);

const personalAccessToken = await prisma.personalAccessToken.update({
const personalAccessToken = await prisma.personalAccessToken.findFirst({
where: {
hashedToken,
revokedAt: null,
},
data: {
lastAccessedAt: new Date(),
},
});

if (!personalAccessToken) {
// The token may have been revoked or is entirely invalid
return;
}

await prisma.personalAccessToken.update({
where: {
id: personalAccessToken.id,
},
data: {
lastAccessedAt: new Date(),
},
});

const decryptedToken = decryptPersonalAccessToken(personalAccessToken);

if (decryptedToken !== token) {
Expand Down Expand Up @@ -210,6 +217,18 @@ export async function createPersonalAccessTokenFromAuthorizationCode(
},
});

if (existingCliPersonalAccessToken.revokedAt) {
// re-activate revoked CLI PAT so we can use it again
await prisma.personalAccessToken.update({
where: {
id: existingCliPersonalAccessToken.id,
},
data: {
revokedAt: null,
},
});
}

//we don't return the decrypted token
return {
id: existingCliPersonalAccessToken.id,
Expand Down
10 changes: 8 additions & 2 deletions packages/cli-v3/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
tracer,
wrapCommandAction,
} from "../cli/common.js";
import { chalkLink } from "../utilities/cliOutput.js";
import { chalkLink, prettyError } from "../utilities/cliOutput.js";
import { readAuthConfigProfile, writeAuthConfigProfile } from "../utilities/configFiles.js";
import { getVersion } from "../utilities/getVersion.js";
import { printInitialBanner } from "../utilities/initialBanner.js";
Expand Down Expand Up @@ -109,10 +109,16 @@ export async function login(options?: LoginOptions): Promise<LoginResult> {
skipTelemetry: !span.isRecording(),
logLevel: logger.loggerLevel,
},
opts.embedded
true
);

if (!whoAmIResult.success) {
prettyError("Whoami failed", whoAmIResult.error);

if (!opts.embedded) {
outro("Login failed");
}

throw new Error(whoAmIResult.error);
} else {
if (!opts.embedded) {
Expand Down
21 changes: 15 additions & 6 deletions packages/cli-v3/src/commands/whoami.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intro, note } from "@clack/prompts";
import { intro, note, outro } from "@clack/prompts";
import { chalkLink } from "../utilities/cliOutput.js";
import { logger } from "../utilities/logger.js";
import { isLoggedIn } from "../utilities/session.js";
Expand Down Expand Up @@ -66,11 +66,20 @@ export async function whoAmI(
if (authentication.error === "fetch failed") {
loadingSpinner.stop("Fetch failed. Platform down?");
} else {
loadingSpinner.stop(
`You must login first. Use \`trigger.dev login --profile ${
options?.profile ?? "default"
}\` to login.`
);
if (embedded) {
loadingSpinner.stop(
`Failed to check account details. You may want to run \`trigger.dev logout --profile ${
options?.profile ?? "default"
}\` and try again.`
);
} else {
loadingSpinner.stop(
`You must login first. Use \`trigger.dev login --profile ${
options?.profile ?? "default"
}\` to login.`
);
outro("Whoami failed");
}
}

return {
Expand Down
23 changes: 23 additions & 0 deletions packages/cli-v3/src/utilities/cliOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ export function prettyPrintDate(date: Date = new Date()) {
return formattedDate;
}

export function prettyError(header: string, body?: string, footer?: string) {
const prefix = "Error: ";
const indent = Array(prefix.length).fill(" ").join("");
const spacing = "\n\n";

const prettyPrefix = chalkError(prefix);

const withIndents = (text?: string) =>
text
?.split("\n")
.map((line) => `${indent}${line}`)
.join("\n");

const prettyBody = withIndents(body);
const prettyFooter = withIndents(footer);

log.error(
`${prettyPrefix}${header}${prettyBody ? `${spacing}${prettyBody}` : ""}${
prettyFooter ? `${spacing}${prettyFooter}` : ""
}`
);
}

export function prettyWarning(header: string, body?: string, footer?: string) {
const prefix = "Warning: ";
const indent = Array(prefix.length).fill(" ").join("");
Expand Down