Skip to content

Commit d2b0045

Browse files
committed
Simplify env auth flow around the /projects endpoints
1 parent f60d4b5 commit d2b0045

File tree

3 files changed

+50
-133
lines changed

3 files changed

+50
-133
lines changed

apps/webapp/app/routes/api.v1.projects.$projectRef.$env.jwt.ts

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { ActionFunctionArgs, json } from "@remix-run/node";
1+
import { type ActionFunctionArgs, json } from "@remix-run/node";
22
import { generateJWT as internal_generateJWT } from "@trigger.dev/core/v3";
33
import { z } from "zod";
4-
import { prisma } from "~/db.server";
5-
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
6-
import { getEnvironmentFromEnv } from "./api.v1.projects.$projectRef.$env";
4+
import {
5+
authenticatedEnvironmentForAuthentication,
6+
authenticateRequest,
7+
} from "~/services/apiAuth.server";
78

89
const ParamsSchema = z.object({
910
projectRef: z.string(),
@@ -20,7 +21,11 @@ const RequestBodySchema = z.object({
2021
});
2122

2223
export async function action({ request, params }: ActionFunctionArgs) {
23-
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
24+
const authenticationResult = await authenticateRequest(request, {
25+
personalAccessToken: true,
26+
organizationAccessToken: true,
27+
apiKey: false,
28+
});
2429

2530
if (!authenticationResult) {
2631
return json({ error: "Invalid or Missing Access Token" }, { status: 401 });
@@ -33,35 +38,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
3338
}
3439

3540
const { projectRef, env } = parsedParams.data;
41+
const triggerBranch = request.headers.get("x-trigger-branch") ?? undefined;
3642

37-
const project = await prisma.project.findFirst({
38-
where: {
39-
externalRef: projectRef,
40-
organization: {
41-
members: {
42-
some: {
43-
userId: authenticationResult.userId,
44-
},
45-
},
46-
},
47-
},
48-
});
49-
50-
if (!project) {
51-
return json({ error: "Project not found" }, { status: 404 });
52-
}
53-
54-
const envResult = await getEnvironmentFromEnv({
55-
projectId: project.id,
56-
userId: authenticationResult.userId,
43+
const runtimeEnv = await authenticatedEnvironmentForAuthentication(
44+
authenticationResult,
45+
projectRef,
5746
env,
58-
});
59-
60-
if (!envResult.success) {
61-
return json({ error: envResult.error }, { status: 404 });
62-
}
63-
64-
const runtimeEnv = envResult.environment;
47+
triggerBranch
48+
);
6549

6650
const parsedBody = RequestBodySchema.safeParse(await request.json());
6751

@@ -72,29 +56,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
7256
);
7357
}
7458

75-
const triggerBranch = request.headers.get("x-trigger-branch") ?? undefined;
76-
77-
let previewBranchEnvironmentId: string | undefined;
78-
79-
if (triggerBranch) {
80-
const previewBranch = await prisma.runtimeEnvironment.findFirst({
81-
where: {
82-
projectId: project.id,
83-
branchName: triggerBranch,
84-
parentEnvironmentId: runtimeEnv.id,
85-
archivedAt: null,
86-
},
87-
});
88-
89-
if (previewBranch) {
90-
previewBranchEnvironmentId = previewBranch.id;
91-
} else {
92-
return json({ error: `Preview branch ${triggerBranch} not found` }, { status: 404 });
93-
}
94-
}
95-
9659
const claims = {
97-
sub: previewBranchEnvironmentId ?? runtimeEnv.id,
60+
sub: runtimeEnv.id,
9861
pub: true,
9962
...parsedBody.data.claims,
10063
};

apps/webapp/app/routes/api.v1.projects.$projectRef.$env.workers.$tagName.ts

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { json, type LoaderFunctionArgs } from "@remix-run/server-runtime";
22
import { z } from "zod";
33
import { $replica, prisma } from "~/db.server";
4-
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
54
import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server";
6-
import { getEnvironmentFromEnv } from "./api.v1.projects.$projectRef.$env";
7-
import { GetWorkerByTagResponse } from "@trigger.dev/core/v3/schemas";
5+
import { type GetWorkerByTagResponse } from "@trigger.dev/core/v3/schemas";
86
import { env as $env } from "~/env.server";
97
import { v3RunsPath } from "~/utils/pathBuilder";
8+
import {
9+
authenticatedEnvironmentForAuthentication,
10+
authenticateRequest,
11+
} from "~/services/apiAuth.server";
1012

1113
const ParamsSchema = z.object({
1214
projectRef: z.string(),
@@ -21,7 +23,11 @@ const HeadersSchema = z.object({
2123
type ParamsSchema = z.infer<typeof ParamsSchema>;
2224

2325
export async function loader({ request, params }: LoaderFunctionArgs) {
24-
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
26+
const authenticationResult = await authenticateRequest(request, {
27+
personalAccessToken: true,
28+
organizationAccessToken: true,
29+
apiKey: false,
30+
});
2531

2632
if (!authenticationResult) {
2733
return json({ error: "Invalid or Missing Access Token" }, { status: 401 });
@@ -32,51 +38,17 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
3238
if (!parsedParams.success) {
3339
return json({ error: "Invalid Params" }, { status: 400 });
3440
}
35-
36-
const parsedHeaders = HeadersSchema.safeParse(Object.fromEntries(request.headers));
37-
38-
const branch = parsedHeaders.success ? parsedHeaders.data["x-trigger-branch"] : undefined;
39-
4041
const { projectRef, env } = parsedParams.data;
4142

42-
const project = await prisma.project.findFirst({
43-
where: {
44-
externalRef: projectRef,
45-
organization: {
46-
members: {
47-
some: {
48-
userId: authenticationResult.userId,
49-
},
50-
},
51-
},
52-
},
53-
select: {
54-
id: true,
55-
slug: true,
56-
organization: {
57-
select: {
58-
slug: true,
59-
},
60-
},
61-
},
62-
});
63-
64-
if (!project) {
65-
return json({ error: "Project not found" }, { status: 404 });
66-
}
43+
const parsedHeaders = HeadersSchema.safeParse(Object.fromEntries(request.headers));
44+
const triggerBranch = parsedHeaders.success ? parsedHeaders.data["x-trigger-branch"] : undefined;
6745

68-
const envResult = await getEnvironmentFromEnv({
69-
projectId: project.id,
70-
userId: authenticationResult.userId,
46+
const runtimeEnv = await authenticatedEnvironmentForAuthentication(
47+
authenticationResult,
48+
projectRef,
7149
env,
72-
branch,
73-
});
74-
75-
if (!envResult.success) {
76-
return json({ error: envResult.error }, { status: 404 });
77-
}
78-
79-
const runtimeEnv = envResult.environment;
50+
triggerBranch
51+
);
8052

8153
const currentWorker = await findCurrentWorkerFromEnvironment(
8254
{
@@ -110,8 +82,8 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
11082

11183
const urls = {
11284
runs: `${$env.APP_ORIGIN}${v3RunsPath(
113-
{ slug: project.organization.slug },
114-
{ slug: project.slug },
85+
{ slug: runtimeEnv.organization.slug },
86+
{ slug: runtimeEnv.project.slug },
11587
{ slug: runtimeEnv.slug },
11688
{ versions: [currentWorker.version] }
11789
)}`,

apps/webapp/app/routes/api.v1.projects.$projectRef.dev-status.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { json, type LoaderFunctionArgs } from "@remix-run/node";
22
import { z } from "zod";
3-
import { prisma } from "~/db.server";
43
import { devPresence } from "~/presenters/v3/DevPresence.server";
5-
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
6-
import { getEnvironmentFromEnv } from "./api.v1.projects.$projectRef.$env";
4+
import {
5+
authenticatedEnvironmentForAuthentication,
6+
authenticateRequest,
7+
} from "~/services/apiAuth.server";
78

89
const ParamsSchema = z.object({
910
projectRef: z.string(),
1011
});
1112

1213
export async function loader({ request, params }: LoaderFunctionArgs) {
13-
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
14+
const authenticationResult = await authenticateRequest(request, {
15+
personalAccessToken: true,
16+
organizationAccessToken: true,
17+
apiKey: false,
18+
});
1419

1520
if (!authenticationResult) {
1621
return json({ error: "Invalid or Missing Access Token" }, { status: 401 });
@@ -24,34 +29,11 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
2429

2530
const { projectRef } = parsedParams.data;
2631

27-
const project = await prisma.project.findFirst({
28-
where: {
29-
externalRef: projectRef,
30-
organization: {
31-
members: {
32-
some: {
33-
userId: authenticationResult.userId,
34-
},
35-
},
36-
},
37-
},
38-
});
39-
40-
if (!project) {
41-
return json({ error: "Project not found" }, { status: 404 });
42-
}
43-
44-
const envResult = await getEnvironmentFromEnv({
45-
projectId: project.id,
46-
userId: authenticationResult.userId,
47-
env: "dev",
48-
});
49-
50-
if (!envResult.success) {
51-
return json({ error: envResult.error }, { status: 404 });
52-
}
53-
54-
const runtimeEnv = envResult.environment;
32+
const runtimeEnv = await authenticatedEnvironmentForAuthentication(
33+
authenticationResult,
34+
projectRef,
35+
"dev"
36+
);
5537

5638
const isConnected = await devPresence.isConnected(runtimeEnv.id);
5739

0 commit comments

Comments
 (0)