Simplifying routes and making new procedures

Hey, I am making an app and I wanted to ask if there was an easier way to ensure that the user has access to the team they are redirecting to. I do this by writing a query to check if the user's id is found inside of the members object of that team. If nothing is returned then I redirect them somewhere else. I am wondering if there is a simpler way to do this, like making a teamProcedure in trpc that automatically does that every single time a procedure call is made. Any tips will help. team/[teamId]/layout.tsx
import { redirect } from "next/navigation";
import Sidebar from "../_components/sidebar";
import { api } from "~/trpc/server";

export default function Layout({
children,
params,
}: {
children: React.ReactNode;
params: { teamId: string };
}) {
const teamAuth = api.team.checkTeamAuth.query({ teamId: params.teamId });
if (!teamAuth) {
redirect("/login");
}
return (
<div className="flex min-h-screen gap-2">
<Sidebar />
{children}
</div>
);
}
import { redirect } from "next/navigation";
import Sidebar from "../_components/sidebar";
import { api } from "~/trpc/server";

export default function Layout({
children,
params,
}: {
children: React.ReactNode;
params: { teamId: string };
}) {
const teamAuth = api.team.checkTeamAuth.query({ teamId: params.teamId });
if (!teamAuth) {
redirect("/login");
}
return (
<div className="flex min-h-screen gap-2">
<Sidebar />
{children}
</div>
);
}
query:
checkTeamAuth: protectedProcedure
.input(
z.object({
teamId: z.string(),
}),
)
.query(async ({ ctx, input }) => {
return ctx.db.team.findFirst({
where: {
name: input.teamId,
members: { some: { id: ctx.session.user.id } },
},
});
}),
checkTeamAuth: protectedProcedure
.input(
z.object({
teamId: z.string(),
}),
)
.query(async ({ ctx, input }) => {
return ctx.db.team.findFirst({
where: {
name: input.teamId,
members: { some: { id: ctx.session.user.id } },
},
});
}),
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server