Skip to content

Commit d7a4b2b

Browse files
committed
isolate middleware functions from trpc configuration
1 parent c300ebf commit d7a4b2b

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

worker/trpc/middlewares/auth.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
import { createClerkClient, type User } from "@clerk/backend";
2-
import { TRPCError } from "@trpc/server";
3-
import { t } from "@worker/trpc";
42
import type { Context } from "../context";
53

64
export type AuthenticatedContext = Context & { user: User };
75

8-
export const authentication = t.middleware(async ({ next, ctx }) => {
9-
try {
10-
const user = await authFunction(ctx);
11-
return next({ ctx: { ...ctx, user } });
12-
} catch (error) {
13-
console.error(error);
14-
throw new TRPCError({ message: "Unable to get auth", cause: error, code: "UNAUTHORIZED" });
15-
}
16-
});
17-
186
export async function authFunction(ctx: Context) {
197
const clerk = createClerkClient({ secretKey: ctx.env.CLERK_SECRET_KEY, publishableKey: ctx.env.CLERK_PUBLISHABLE_KEY });
208
const requestState = await clerk.authenticateRequest(ctx.req);

worker/trpc/middlewares/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { TRPCError } from "@trpc/server";
2+
import { t } from "@worker/trpc";
3+
import { authFunction } from "@worker/trpc/middlewares/auth";
4+
import { rateLimitFunction } from "@worker/trpc/middlewares/rate-limit";
5+
6+
export const authentication = t.middleware(async ({ next, ctx }) => {
7+
try {
8+
const user = await authFunction(ctx);
9+
return next({ ctx: { ...ctx, user } });
10+
} catch (error) {
11+
console.error(error);
12+
throw new TRPCError({ message: "Unable to get auth", cause: error, code: "UNAUTHORIZED" });
13+
}
14+
});
15+
16+
export const rateLimit = t.middleware(async ({ next, ctx }) => {
17+
await rateLimitFunction(ctx)
18+
return next({ ctx });
19+
});

worker/trpc/middlewares/rate-limit.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { TRPCError } from "@trpc/server";
22
import { Ratelimit } from "@upstash/ratelimit";
3-
import { t } from "@worker/trpc";
43
import type { Context } from "@worker/trpc/context";
54
import { isLocal } from "@worker/trpc/util/local";
65

@@ -32,8 +31,3 @@ export async function rateLimitFunction(ctx: Context) {
3231
});
3332
}
3433
}
35-
36-
export const rateLimit = t.middleware(async ({ next, ctx }) => {
37-
await rateLimitFunction(ctx)
38-
return next({ ctx });
39-
});

worker/trpc/procedures.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { t } from "@worker/trpc";
2-
import { authentication } from "@worker/trpc/middlewares/auth";
3-
import { rateLimit } from "@worker/trpc/middlewares/rate-limit";
2+
import { rateLimit, authentication } from "@worker/trpc/middlewares";
43

54
export const publicProcedure = t.procedure.use(rateLimit);
65
export const privateProcedure = t.procedure.use(rateLimit).use(authentication);

0 commit comments

Comments
 (0)