Skip to content

Commit 5b18298

Browse files
authored
Multiple machine CLI support (#1022)
* If a cli PAT exists, associate the authorization code with it * Improved the error messages
1 parent 7a9bd18 commit 5b18298

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

apps/webapp/app/routes/api.v1.token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function action({ request }: ActionFunctionArgs) {
2323
const anyBody = await request.json();
2424
const body = GetPersonalAccessTokenRequestSchema.safeParse(anyBody);
2525
if (!body.success) {
26-
return json({ message: generateErrorMessage(body.error.issues) }, { status: 422 });
26+
return json({ error: generateErrorMessage(body.error.issues) }, { status: 422 });
2727
}
2828

2929
try {
@@ -45,6 +45,6 @@ export async function action({ request }: ActionFunctionArgs) {
4545
return json({ error: error.message }, { status: 400 });
4646
}
4747

48-
return json({ error: "Something went wrong" }, { status: 500 });
48+
return json({ error: "Something went wrong" }, { status: 400 });
4949
}
5050
}

apps/webapp/app/routes/api.v2.whoami.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,34 @@ import { authenticateApiRequestWithPersonalAccessToken } from "~/services/person
88

99
export async function loader({ request }: LoaderFunctionArgs) {
1010
logger.info("whoami v2", { url: request.url });
11+
try {
12+
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
13+
if (!authenticationResult) {
14+
return json({ error: "Invalid or Missing Access Token" }, { status: 401 });
15+
}
1116

12-
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
13-
if (!authenticationResult) {
14-
return json({ error: "Invalid or Missing Access Token" }, { status: 401 });
15-
}
17+
const user = await prisma.user.findUnique({
18+
select: {
19+
email: true,
20+
},
21+
where: {
22+
id: authenticationResult.userId,
23+
},
24+
});
1625

17-
const user = await prisma.user.findUnique({
18-
select: {
19-
email: true,
20-
},
21-
where: {
22-
id: authenticationResult.userId,
23-
},
24-
});
26+
if (!user) {
27+
return json({ error: "User not found" }, { status: 404 });
28+
}
2529

26-
if (!user) {
27-
return json({ error: "User not found" }, { status: 404 });
30+
const result: WhoAmIResponse = {
31+
userId: authenticationResult.userId,
32+
email: user.email,
33+
dashboardUrl: env.APP_ORIGIN,
34+
};
35+
return json(result);
36+
} catch (error) {
37+
const errorMessage = error instanceof Error ? error.message : "Something went wrong";
38+
logger.error("Error in whoami v2", { error: errorMessage });
39+
return json({ error: errorMessage }, { status: 400 });
2840
}
29-
30-
const result: WhoAmIResponse = {
31-
userId: authenticationResult.userId,
32-
email: user.email,
33-
dashboardUrl: env.APP_ORIGIN,
34-
};
35-
return json(result);
3641
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { PersonalAccessToken } from "@trigger.dev/database";
12
import { customAlphabet, nanoid } from "nanoid";
23
import nodeCrypto from "node:crypto";
34
import { z } from "zod";
45
import { prisma } from "~/db.server";
56
import { env } from "~/env.server";
67
import { logger } from "./logger.server";
7-
import { PersonalAccessToken } from "@trigger.dev/database";
88

99
const tokenValueLength = 40;
1010
//lowercase only, removed 0 and l to avoid confusion
@@ -54,7 +54,6 @@ export async function getPersonalAccessTokenFromAuthorizationCode(authorizationC
5454
},
5555
where: {
5656
code: authorizationCode,
57-
5857
createdAt: {
5958
gte: tenMinutesAgo,
6059
},
@@ -199,13 +198,25 @@ export async function createPersonalAccessTokenFromAuthorizationCode(
199198
},
200199
});
201200

202-
//we only allow you to have one CLI PAT at a time
201+
//we only allow you to have one CLI PAT at a time, so return this
203202
if (existingCliPersonalAccessToken) {
204-
await prisma.personalAccessToken.delete({
203+
//associate this authorization code with the existing personal access token
204+
await prisma.authorizationCode.update({
205205
where: {
206-
id: existingCliPersonalAccessToken.id,
206+
code: authorizationCode,
207+
},
208+
data: {
209+
personalAccessTokenId: existingCliPersonalAccessToken.id,
207210
},
208211
});
212+
213+
//we don't return the decrypted token
214+
return {
215+
id: existingCliPersonalAccessToken.id,
216+
name: existingCliPersonalAccessToken.name,
217+
userId: existingCliPersonalAccessToken.userId,
218+
obfuscateToken: existingCliPersonalAccessToken.obfuscatedToken,
219+
};
209220
}
210221

211222
const token = await createPersonalAccessToken({

0 commit comments

Comments
 (0)