How to modify useSession data type?

I am using credentials and providers then returning user, I was trying to put some user infos into session but session.user always returning
{
user: {
name: '1234',
image: null,
id: 'clb87p1tw0000vxqsb4kij8qw'
},
expires: '2022-12-03T18:21:49.726Z'
}
{
user: {
name: '1234',
image: null,
id: 'clb87p1tw0000vxqsb4kij8qw'
},
expires: '2022-12-03T18:21:49.726Z'
}
37 Replies
cje
cje2y ago
id is a field that create-t3-app adds to the session. look in [nextauth].ts and next-auth.d.ts to see how we do it, you can do the same for any other data that being said be careful with this - only send stuff to the frontend that you're ok with the user seeing
Çağlar
ÇağlarOP2y ago
I did a lot of changing on that type file Nothing happened 😄
Çağlar
ÇağlarOP2y ago
Çağlar
ÇağlarOP2y ago
it should return only id right?
Çağlar
ÇağlarOP2y ago
It is returning the same type of object
Çağlar
ÇağlarOP2y ago
this is where I log
cje
cje2y ago
the interface doesn't determine what is returned it's just a typedef types don't exist at runtime
Çağlar
ÇağlarOP2y ago
Yes exactly I couldnt find where I modify returnin object
cje
cje2y ago
you already posted it 😉 the session callback
Çağlar
ÇağlarOP2y ago
That made me confuse 😄
cje
cje2y ago
cje
cje2y ago
theres probably a nicer way to write it but this works
cje
cje2y ago
Çağlar
ÇağlarOP2y ago
I am wondering
cje
cje2y ago
"Logged in as" doesn't show a name because it's not being passed anymore
Çağlar
ÇağlarOP2y ago
something
Çağlar
ÇağlarOP2y ago
Çağlar
ÇağlarOP2y ago
where do these come from ?
EQ
EQ2y ago
Do you also know how to do it for the authorize function?
EQ
EQ2y ago
It expects a User object from NextAuth itself or null But when I override, it overrides only for Session and not User The error only goes away if I return as
return {
email: user.email
} as any
return {
email: user.email
} as any
cje
cje2y ago
havent used it much tbh maybe ask next-auth people we just glue the libraries together 😅
EQ
EQ2y ago
ah okay, thanks haha
Çağlar
ÇağlarOP2y ago
@cje do you know where is this come from? sorry for bother you 😄
cje
cje2y ago
you mean where the user data comes from?
Çağlar
ÇağlarOP2y ago
I am getting the user data from prisma with credentials and then returning it jwt taking the user object
Çağlar
ÇağlarOP2y ago
Çağlar
ÇağlarOP2y ago
my jwt user object returning me whole user infos but I am using only user id
Çağlar
ÇağlarOP2y ago
this is the new session.user object
Çağlar
ÇağlarOP2y ago
this is my token object but it doesnt get surname from user object
cje
cje2y ago
yea of course not log token at the start of your jwt function see what it has you're adding id to it only in the function why would it have surname
Çağlar
ÇağlarOP2y ago
where are the others comes from? 😄 like I am giving only id I am expecting that my token includes only user id
Çağlar
ÇağlarOP2y ago
Çağlar
ÇağlarOP2y ago
I couldnt understand that where others come from
Çağlar
ÇağlarOP2y ago
hmm, thank you very much @cje
yemirhan
yemirhan2y ago
in your next-auth.d.ts
import type { Role } from "@prisma/client";
import type { DefaultSession, DefaultUser } from "next-auth";

declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user?: {
id: string;
email: string;
image: string | null,
name: string | null,
role: Role
} & DefaultSession["user"];
}
interface User extends DefaultUser {
role: Role
}
}
import type { Role } from "@prisma/client";
import type { DefaultSession, DefaultUser } from "next-auth";

declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user?: {
id: string;
email: string;
image: string | null,
name: string | null,
role: Role
} & DefaultSession["user"];
}
interface User extends DefaultUser {
role: Role
}
}
Çağlar
ÇağlarOP2y ago
as cje said that "the interface doesn't determine what is returned" I guess the only way to change what jwt includes is decode jwt and update the data and then encode again
Want results from more Discord servers?
Add your server