CodexC
tRPC2y ago
2 replies
Codex

Context is not being passed correctly

I will include my setup at the end. My problem is that I am using nextauth with database sessions and passing the session as context into the procedures; however, everything that I have added myself isn't being included in the context for some reason (user.id and user.role)

This is my session type I declared,
declare module "next-auth" {
  interface Session extends DefaultSession {
    user: DefaultSession["user"] & {
      id: string;
      image: string;
      role: string;
    };
  }

  interface User {
    id: string;
    image: string;
    role: string;
  }
}


This is the session callback I am using.
session: async ({ session, user }) => {
  if (!gymId) {
    return {
      expires: session.expires,
      user: {
        id: user.id,
        role: "user",
        image: user.image,
      },
    };
  }
  const role = await prisma.roles.findUnique({
    where: {
      gymId_userId: {
        gymId: gymId.id,
        userId: user.id,
      },
    },
  });
  session = {
    expires: session.expires,
    user: {
      id: user.id,
      role: role?.name ?? "user",
      image: user.image,
    },
  };
  console.log("Fetching session: ")
  console.log(session)
  return session;
}

// This is a function that I will use when creating the context
export const getServerAuthSession = (ctx: {
  req: GetServerSidePropsContext["req"];
  res: GetServerSidePropsContext["res"];
}) => {
  return getServerSession(ctx.req, ctx.res, authOptions);
};

The console.log is just to see what is happening where (I will include it at the end)
I will include the rest of the information in the next message as I reached the character limit.
Was this page helpful?