GabrielDSFS
Explore posts from serversTTCTheo's Typesafe Cult
•Created by GabrielDSFS on 5/8/2024 in #questions
How to make a fully public page with TRPC requests on T3?
This here is the middleware @Vanxh
import { authMiddleware, clerkClient, redirectToSignIn } from "@clerk/nextjs";
import { NextResponse } from "next/server";
// This example protects all routes including api/trpc routes
// Please edit this to allow other routes to be public as needed.
// See https://clerk.com/docs/references/nextjs/auth-middleware for more information about configuring your Middleware
export default authMiddleware({
ignoredRoutes: ["/api/webhooks/stripe"],
async afterAuth(auth, req, evt) {
// Handle users who aren't authenticated
if (!auth.userId && !auth.isPublicRoute) {
return redirectToSignIn({ returnBackUrl: req.url });
}
if (auth.userId) {
const user = await clerkClient.users.getUser(auth.userId);
if (!user?.publicMetadata?.credits) {
await clerkClient.users.updateUserMetadata(auth.userId, {
publicMetadata: {
credits: 5000,
},
});
}
if (!user?.publicMetadata?.plan) {
await clerkClient.users.updateUserMetadata(auth.userId, {
publicMetadata: {
plan: "basic",
},
});
}
}
// If the user is signed in and trying to access a protected route, allow them to access route
if (auth.userId && !auth.isPublicRoute) {
return NextResponse.next();
}
// Allow users visiting public routes to access them
return NextResponse.next();
},
});
export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};
11 replies
TTCTheo's Typesafe Cult
•Created by GabrielDSFS on 5/8/2024 in #questions
How to make a fully public page with TRPC requests on T3?
Hey, yeah it's a public procedure @fotoflo
export const publicProcedure = t.procedure.use(sentryMiddleware);
getSinglePublicByID: publicProcedure
.input(ThreadCommonSchema)
.query(async ({ input, ctx }) => {
try {
return ctx.prisma.thread.findUnique({
where: { id: input.id, isPublic: true },
include: { messages: true },
});
} catch (err) {
handlePrismaTRPCError(err);
console.log("error is", err);
}
}),
11 replies