Public and Private procedures Not working with Clerk and Prisma

I have initialized a create-t3-app. I want to use clerk as my auth provider. I have ran into a problem midway when setting up the middleware and making changes to the prisma client. Now this is what the api calls are returning
11 Replies
Burhan
BurhanOP10mo ago
No description
Burhan
BurhanOP10mo ago
import { TRPCError, type inferAsyncReturnType, initTRPC } from "@trpc/server";
import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
import { getAuth, type SignedInAuthObject, type SignedOutAuthObject } from '@clerk/nextjs/server';
import superjson from "superjson";
import { ZodError } from "zod";

import { prisma } from "~/server/prisma";

type CreateContextOptions = Record<string, never>;


interface AuthContext {
auth: SignedInAuthObject | SignedOutAuthObject;
}

export const createContextInner = async ({ auth }: AuthContext ) => {
return {
auth,
}
}


// const createInnerTRPCContext = (_opts: CreateContextOptions) => {
// return {
// prisma,
// };
// };

// export const createTRPCContext = (opts: CreateNextContextOptions) => {
// return {
// ...createInnerTRPCContext({}),
// req: opts.req,
// };
// };

export const createContext = async (opts: CreateNextContextOptions) => {
return await createContextInner({ auth: getAuth(opts.req) })
}


const t = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter({ shape }) {
return shape
}
})

export const createTRPCRouter = t.router;
export type Context = inferAsyncReturnType<typeof createContext>;

export const publicProcedure = t.procedure;


const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.auth.userId) {
throw new TRPCError({ code: 'UNAUTHORIZED' })
}
return next({
ctx: {
auth: ctx.auth,
},
})
})

// export this procedure to be used anywhere in your application
export const protectedProcedure = t.procedure.use(isAuthed)

export const mergeRouters = t.mergeRouters;
import { TRPCError, type inferAsyncReturnType, initTRPC } from "@trpc/server";
import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
import { getAuth, type SignedInAuthObject, type SignedOutAuthObject } from '@clerk/nextjs/server';
import superjson from "superjson";
import { ZodError } from "zod";

import { prisma } from "~/server/prisma";

type CreateContextOptions = Record<string, never>;


interface AuthContext {
auth: SignedInAuthObject | SignedOutAuthObject;
}

export const createContextInner = async ({ auth }: AuthContext ) => {
return {
auth,
}
}


// const createInnerTRPCContext = (_opts: CreateContextOptions) => {
// return {
// prisma,
// };
// };

// export const createTRPCContext = (opts: CreateNextContextOptions) => {
// return {
// ...createInnerTRPCContext({}),
// req: opts.req,
// };
// };

export const createContext = async (opts: CreateNextContextOptions) => {
return await createContextInner({ auth: getAuth(opts.req) })
}


const t = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter({ shape }) {
return shape
}
})

export const createTRPCRouter = t.router;
export type Context = inferAsyncReturnType<typeof createContext>;

export const publicProcedure = t.procedure;


const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.auth.userId) {
throw new TRPCError({ code: 'UNAUTHORIZED' })
}
return next({
ctx: {
auth: ctx.auth,
},
})
})

// export this procedure to be used anywhere in your application
export const protectedProcedure = t.procedure.use(isAuthed)

export const mergeRouters = t.mergeRouters;
This is where the error is arising from
Matvey
Matvey10mo ago
in the middleware you need to make api routes public trpc works by making requests to /api/trpc, and clerk instead of trpc response, responds with the login form
Burhan
BurhanOP10mo ago
import { authMiddleware } from "@clerk/nextjs";

export default authMiddleware({
// Routes that can be accessed while signed out
publicRoutes: ['/anyone-can-visit-this-route', '/api/trpc/'],
// Routes that can always be accessed, and have
// no authentication information
ignoredRoutes: ['/no-auth-in-this-route'],
});

export const config = {
// Protects all routes, including api/trpc.
// See https://clerk.com/docs/references/nextjs/auth-middleware
// for more information about configuring your Middleware
// matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};
import { authMiddleware } from "@clerk/nextjs";

export default authMiddleware({
// Routes that can be accessed while signed out
publicRoutes: ['/anyone-can-visit-this-route', '/api/trpc/'],
// Routes that can always be accessed, and have
// no authentication information
ignoredRoutes: ['/no-auth-in-this-route'],
});

export const config = {
// Protects all routes, including api/trpc.
// See https://clerk.com/docs/references/nextjs/auth-middleware
// for more information about configuring your Middleware
// matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};
Like this? @Matvey This still isnt working tho
Matvey
Matvey10mo ago
publicRoutes: ["/(api|trpc)(.*)"],
publicRoutes: ["/(api|trpc)(.*)"],
Burhan
BurhanOP10mo ago
Still getting the unexpected token error
Matvey
Matvey10mo ago
strange, can you share the repo?
Burhan
BurhanOP10mo ago
GitHub
GitHub - BurhanH12/Shop-Sphere: Shop Sphere: Revolutionize your sho...
Shop Sphere: Revolutionize your shopping experience - BurhanH12/Shop-Sphere
Burhan
BurhanOP10mo ago
I followed this documentation
Burhan
BurhanOP10mo ago
Integrate Clerk into your Next.js Pages Router app with tRPC | Clerk
Learn how to integrate Clerk into your Next.js application using tRPC. tRPC can be used with Clerk, but requires a few tweaks from a traditional Clerk + Next.js setup.
Burhan
BurhanOP10mo ago
@Matvey Anyone got an idea?
Want results from more Discord servers?
Add your server