Kseikyo
Kseikyo
TTCTheo's Typesafe Cult
Created by Kseikyo on 9/2/2023 in #questions
Context on middleware always undefined
I'm trying to use trpc with next-auth using the GoogleProvider. I can sign in just fine, publicProcedures work, but all protectedProcedures will throw UNAUTHORIZED, even though when using the useSession hook on the same component, it returns the correct data. I can also see the session on prisma studio.
export const createTRPCContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

// Get the session from the server using the getServerSession wrapper function
const session = await getServerAuthSession({ req, res });
console.log("session createTRPCContext", session); //logs the session just fine

return createInnerTRPCContext({
session,
});
};

...

const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
// always throws on the use of protectedProcedure
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({
code: "UNAUTHORIZED",
cause: JSON.stringify(ctx), // cause is undefined on stacktrace
message: "You must be logged in to perform this action.",
});
}
return next({
ctx: {
// infers the `session` as non-nullable
session: { ...ctx.session, user: ctx.session.user },
},
});
});
export const createTRPCContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

// Get the session from the server using the getServerSession wrapper function
const session = await getServerAuthSession({ req, res });
console.log("session createTRPCContext", session); //logs the session just fine

return createInnerTRPCContext({
session,
});
};

...

const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
// always throws on the use of protectedProcedure
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({
code: "UNAUTHORIZED",
cause: JSON.stringify(ctx), // cause is undefined on stacktrace
message: "You must be logged in to perform this action.",
});
}
return next({
ctx: {
// infers the `session` as non-nullable
session: { ...ctx.session, user: ctx.session.user },
},
});
});
6 replies
TTCTheo's Typesafe Cult
Created by Kseikyo on 8/28/2023 in #questions
TRPC and protectedProcedure with next-auth
Has anyone ever experienced an issue with a new t3 app with next-auth and trpc where a protectedProcedure for a mutation stays pending and multiple session requests are also pending when you're signed in? I've also added a log to the middleware on the enforceUserIsAuthed and it never seems to get called. Also, when this happens I have to remove the .next folder and start the dev server again, otherwise clearing the site data and hard refreshing doesn't even load the page anymore. The procedure works when it's a publicProcedure
test: protectedProcedure.mutation(({ ctx }) => {
console.log("ctx", ctx);
return { data: ctx.session.user, status: 200 };
}),
test: protectedProcedure.mutation(({ ctx }) => {
console.log("ctx", ctx);
return { data: ctx.session.user, status: 200 };
}),
4 replies
TTCTheo's Typesafe Cult
Created by Kseikyo on 9/25/2022 in #questions
Prisma nexth-auth role based access control model
Hi, just getting started with the t3-stack and prisma. I'm a little confused on why the Account model is required for next-auth. Also, I'm not sure how to add a role that would give me type definitions when using the session. Yes, I did looked it up and I followed the tutorial from next-auth, but that doesn't give me the type definitions on the user when setting up the session. https://next-auth.js.org/tutorials/role-based-login-strategy Also, I've already changed the next-auth.d.ts file to include the role.
declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user?: {
id: string;
role: string;
} & DefaultSession["user"];
}
}
declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user?: {
id: string;
role: string;
} & DefaultSession["user"];
}
}
Edit-1: Add next-auth types info;
4 replies