Skip to content

Commit 74d1e61

Browse files
authored
v3: fix for fresh CLI logins after CLI token revoke (#1056)
* prettier login command output * fix for trying to login again after revoking cli token * changeset * improve errors when logging in with revoked or invalid token * fix builds.. again
1 parent 12c83a5 commit 74d1e61

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

.changeset/rich-kangaroos-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"trigger.dev": patch
3+
---
4+
5+
Fix a bug where revoking the CLI token would prevent you from ever logging in again with the CLI.

apps/webapp/app/services/personalAccessToken.server.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,27 @@ export async function authenticatePersonalAccessToken(
134134

135135
const hashedToken = hashToken(token);
136136

137-
const personalAccessToken = await prisma.personalAccessToken.update({
137+
const personalAccessToken = await prisma.personalAccessToken.findFirst({
138138
where: {
139139
hashedToken,
140140
revokedAt: null,
141141
},
142-
data: {
143-
lastAccessedAt: new Date(),
144-
},
145142
});
146143

147144
if (!personalAccessToken) {
145+
// The token may have been revoked or is entirely invalid
148146
return;
149147
}
150148

149+
await prisma.personalAccessToken.update({
150+
where: {
151+
id: personalAccessToken.id,
152+
},
153+
data: {
154+
lastAccessedAt: new Date(),
155+
},
156+
});
157+
151158
const decryptedToken = decryptPersonalAccessToken(personalAccessToken);
152159

153160
if (decryptedToken !== token) {
@@ -210,6 +217,18 @@ export async function createPersonalAccessTokenFromAuthorizationCode(
210217
},
211218
});
212219

220+
if (existingCliPersonalAccessToken.revokedAt) {
221+
// re-activate revoked CLI PAT so we can use it again
222+
await prisma.personalAccessToken.update({
223+
where: {
224+
id: existingCliPersonalAccessToken.id,
225+
},
226+
data: {
227+
revokedAt: null,
228+
},
229+
});
230+
}
231+
213232
//we don't return the decrypted token
214233
return {
215234
id: existingCliPersonalAccessToken.id,

packages/cli-v3/src/commands/login.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
tracer,
1414
wrapCommandAction,
1515
} from "../cli/common.js";
16-
import { chalkLink } from "../utilities/cliOutput.js";
16+
import { chalkLink, prettyError } from "../utilities/cliOutput.js";
1717
import { readAuthConfigProfile, writeAuthConfigProfile } from "../utilities/configFiles.js";
1818
import { getVersion } from "../utilities/getVersion.js";
1919
import { printInitialBanner } from "../utilities/initialBanner.js";
@@ -109,10 +109,16 @@ export async function login(options?: LoginOptions): Promise<LoginResult> {
109109
skipTelemetry: !span.isRecording(),
110110
logLevel: logger.loggerLevel,
111111
},
112-
opts.embedded
112+
true
113113
);
114114

115115
if (!whoAmIResult.success) {
116+
prettyError("Whoami failed", whoAmIResult.error);
117+
118+
if (!opts.embedded) {
119+
outro("Login failed");
120+
}
121+
116122
throw new Error(whoAmIResult.error);
117123
} else {
118124
if (!opts.embedded) {

packages/cli-v3/src/commands/whoami.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { intro, note } from "@clack/prompts";
1+
import { intro, note, outro } from "@clack/prompts";
22
import { chalkLink } from "../utilities/cliOutput.js";
33
import { logger } from "../utilities/logger.js";
44
import { isLoggedIn } from "../utilities/session.js";
@@ -66,11 +66,20 @@ export async function whoAmI(
6666
if (authentication.error === "fetch failed") {
6767
loadingSpinner.stop("Fetch failed. Platform down?");
6868
} else {
69-
loadingSpinner.stop(
70-
`You must login first. Use \`trigger.dev login --profile ${
71-
options?.profile ?? "default"
72-
}\` to login.`
73-
);
69+
if (embedded) {
70+
loadingSpinner.stop(
71+
`Failed to check account details. You may want to run \`trigger.dev logout --profile ${
72+
options?.profile ?? "default"
73+
}\` and try again.`
74+
);
75+
} else {
76+
loadingSpinner.stop(
77+
`You must login first. Use \`trigger.dev login --profile ${
78+
options?.profile ?? "default"
79+
}\` to login.`
80+
);
81+
outro("Whoami failed");
82+
}
7483
}
7584

7685
return {

packages/cli-v3/src/utilities/cliOutput.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ export function prettyPrintDate(date: Date = new Date()) {
6565
return formattedDate;
6666
}
6767

68+
export function prettyError(header: string, body?: string, footer?: string) {
69+
const prefix = "Error: ";
70+
const indent = Array(prefix.length).fill(" ").join("");
71+
const spacing = "\n\n";
72+
73+
const prettyPrefix = chalkError(prefix);
74+
75+
const withIndents = (text?: string) =>
76+
text
77+
?.split("\n")
78+
.map((line) => `${indent}${line}`)
79+
.join("\n");
80+
81+
const prettyBody = withIndents(body);
82+
const prettyFooter = withIndents(footer);
83+
84+
log.error(
85+
`${prettyPrefix}${header}${prettyBody ? `${spacing}${prettyBody}` : ""}${
86+
prettyFooter ? `${spacing}${prettyFooter}` : ""
87+
}`
88+
);
89+
}
90+
6891
export function prettyWarning(header: string, body?: string, footer?: string) {
6992
const prefix = "Warning: ";
7093
const indent = Array(prefix.length).fill(" ").join("");

0 commit comments

Comments
 (0)