for next-auth how to include a different model to a User Object

user {
id: 'cla6t4y450000rpsvp6eijgdh',
name: 'Batman',
email: 'batman@gmail.com',
emailVerified: null,
image: 'https://lh3.googleusercontent.com/a/ALm5wu0MqN4wvAyLfGEz5Q6JlttF_yrj5Mti4NUWX-qh=s96-c',
createdAt: 2022-11-07T13:16:46.132Z,
updatedAt: 2022-11-07T13:16:46.132Z
}
user {
id: 'cla6t4y450000rpsvp6eijgdh',
name: 'Batman',
email: 'batman@gmail.com',
emailVerified: null,
image: 'https://lh3.googleusercontent.com/a/ALm5wu0MqN4wvAyLfGEz5Q6JlttF_yrj5Mti4NUWX-qh=s96-c',
createdAt: 2022-11-07T13:16:46.132Z,
updatedAt: 2022-11-07T13:16:46.132Z
}
What i want
user {
id: 'cla6t4y450000rpsvp6eijgdh',
name: 'Batman',
email: 'batman@gmail.com',
emailVerified: null,
image: 'http://example.com',
teacher: {} <----- another object
createdAt: 2022-11-07T13:16:46.132Z,
updatedAt: 2022-11-07T13:16:46.132Z
}
user {
id: 'cla6t4y450000rpsvp6eijgdh',
name: 'Batman',
email: 'batman@gmail.com',
emailVerified: null,
image: 'http://example.com',
teacher: {} <----- another object
createdAt: 2022-11-07T13:16:46.132Z,
updatedAt: 2022-11-07T13:16:46.132Z
}
so that i can attach the user. teacher in the
async session({ session, user }) {
if (session.user) {
session.user.id = user.id;
session.user.teacher = user.teacher;
}

return session;
},
async session({ session, user }) {
if (session.user) {
session.user.id = user.id;
session.user.teacher = user.teacher;
}

return session;
},
The goal is to use that teacher object to verify certain things. Thanks
39 Replies
Lopen
Lopen2y ago
1:
"/src/types/next-auth.d.ts"

declare module "next-auth" {
interface Session {
user: {
...,
teacher: {},
} & DefaultSession["user"];
}
}
"/src/types/next-auth.d.ts"

declare module "next-auth" {
interface Session {
user: {
...,
teacher: {},
} & DefaultSession["user"];
}
}
2:
"/src/pages/api/auth/[...nextauth].ts"

async session({ session, user }) {
if (session.user) {
session.user.id = user.id;
session.user.teacher = user.teacher;
}

return session;
},
"/src/pages/api/auth/[...nextauth].ts"

async session({ session, user }) {
if (session.user) {
session.user.id = user.id;
session.user.teacher = user.teacher;
}

return session;
},
kacakthegreat
kacakthegreat2y ago
Oh thanks for the answer, I think i need to be more specific next time
user: {
...,
teacher: {},
} & DefaultSession["user"];
user: {
...,
teacher: {},
} & DefaultSession["user"];
The above code solves the problem which it will include the teacher object but if you take a look at my model
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
teacher Teacher?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
teacher Teacher?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
I added a relation to a teacher model So even i added the teacher object to the session, it will undefined
julius
julius2y ago
You need to fetch it from db in your createContext I believe?
kacakthegreat
kacakthegreat2y ago
Where do i do that @julius Thanks for helping Im assuming its here
export const createContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

// Get the session from the server using the unstable_getServerSession wrapper function
const session = await getServerAuthSession({ req, res });

return await createContextInner({
session,
});
};
export const createContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

// Get the session from the server using the unstable_getServerSession wrapper function
const session = await getServerAuthSession({ req, res });

return await createContextInner({
session,
});
};
export const createContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

// Get the session from the server using the unstable_getServerSession wrapper function
const session = await getServerAuthSession({ req, res });

if (session) {
const user = await prisma.user.findUnique({
where: {
id: session?.user?.id,
},
include: {
teacher: true,
},
});

console.log();

session.user!.teacher = user?.teacher;

console.log("session user", user);
console.log("session teacher", session.user);
}

console.log("final session", session);

return await createContextInner({
session,
});
};
export const createContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

// Get the session from the server using the unstable_getServerSession wrapper function
const session = await getServerAuthSession({ req, res });

if (session) {
const user = await prisma.user.findUnique({
where: {
id: session?.user?.id,
},
include: {
teacher: true,
},
});

console.log();

session.user!.teacher = user?.teacher;

console.log("session user", user);
console.log("session teacher", session.user);
}

console.log("final session", session);

return await createContextInner({
session,
});
};
but it didnt attach it to the
useSession()
useSession()
@julius
julius
julius2y ago
The useSession uses the object from the session callback you changed previously. But imo I wouldn’t attach to much stuff to the useSession hook
kacakthegreat
kacakthegreat2y ago
Tried doing it in the callback session also but i dont want to call prisma there haha
julius
julius2y ago
Just do a user.me procedure or smth And you can attach the teacher there The session object is stored in cookies (I think?) and you don’t want too big object there
kacakthegreat
kacakthegreat2y ago
Sorry i dont understand the user.me procedure part . Do you have any examples that you can share if you dont mind
julius
julius2y ago
Gimme sec at my pc soon
kacakthegreat
kacakthegreat2y ago
Okay julius take ur time, thanks for helping
julius
julius2y ago
me: protectedProcedure.query(({ ctx }) => {
const userWithExamples = ctx.prisma.user.findMany({
where: { id: ctx.session.user.id },
include: { examples: true },
});

return userWithExamples;
}),
me: protectedProcedure.query(({ ctx }) => {
const userWithExamples = ctx.prisma.user.findMany({
where: { id: ctx.session.user.id },
include: { examples: true },
});

return userWithExamples;
}),
kacakthegreat
kacakthegreat2y ago
This is under trpc router right?
julius
julius2y ago
yea i placed it just in the approuter now but if you have a user router that's prob better consider that the createContext function is called on every request so you want it to be as minimal as possible to not block unnecessary
kacakthegreat
kacakthegreat2y ago
Okay understood, the thing is i need the teacher object to present in the session globally so that i can use it whenever i can like in Navbar etc... so example
{session.user.teacher ? (<>Teacher</>) : (<>Sign up as a teacher</>)}
{session.user.teacher ? (<>Teacher</>) : (<>Sign up as a teacher</>)}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server