Skip to content

Commit

Permalink
Merge pull request #418 from captableinc/trpc-sentry-middleware
Browse files Browse the repository at this point in the history
feat: use trpc sentry middleware
  • Loading branch information
dahal committed Jul 7, 2024
2 parents e9b6d53 + 45b997b commit e485ed1
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/trpc/api/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TRPCError, initTRPC } from "@trpc/server";
import superjson from "superjson";
import { ZodError } from "zod";

import { isSentryEnabled } from "@/constants/sentry";
import { getIp, getUserAgent } from "@/lib/headers";
import { getServerAuthSession } from "@/server/auth";
import { db } from "@/server/db";
Expand Down Expand Up @@ -93,17 +94,42 @@ const t = initTRPC.context<typeof createTRPCContext>().create({
*/
export const createTRPCRouter = t.router;

const sentryMiddleware = t.middleware(
Sentry.trpcMiddleware({
attachRpcInput: true,
}),
);

const enforceAuthMiddleware = t.middleware(({ ctx: ctx_, next }) => {
const ctx = withAuthTrpcContext(ctx_);

const { email, id } = ctx.session.user;

if (isSentryEnabled) {
Sentry.setUser({ id, ...(email && { email }) });
}

return next({
ctx,
});
});

const pipedSentryMiddleware = sentryMiddleware.unstable_pipe(
enforceAuthMiddleware,
);

const authMiddleware = isSentryEnabled
? pipedSentryMiddleware
: enforceAuthMiddleware;

/**
* Public (unauthenticated) procedure
*
* This is the base piece you use to build new queries and mutations on your tRPC API. It does not
* guarantee that a user querying is authorized, but you can still access user session data if they
* are logged in.
*/
export const withoutAuth = t.procedure.use(({ path, next }) => {
trackMetrics({ path });
return next();
});
export const withoutAuth = t.procedure;

/**
* Protected (authenticated) procedure
Expand All @@ -113,23 +139,4 @@ export const withoutAuth = t.procedure.use(({ path, next }) => {
*
* @see https://trpc.io/docs/procedures
*/
export const withAuth = t.procedure.use(({ ctx: ctx_, next, path }) => {
const ctx = withAuthTrpcContext(ctx_);

const { email, name } = ctx.session.user;
trackMetrics({ path, email: email || "", name: name || "" });

return next({
ctx,
});
});

// Sends metrics to Sentry

const trackMetrics = ({
path,
name,
email,
}: { path: string; name?: string; email?: string }) => {
Sentry.metrics.increment(path, 1, { tags: { email, name } });
};
export const withAuth = t.procedure.use(authMiddleware);

0 comments on commit e485ed1

Please sign in to comment.