admiralackbar411
admiralackbar411
TTCTheo's Typesafe Cult
Created by admiralackbar411 on 6/7/2023 in #questions
How to get Next-Auth session to include custom properties
Thank you! This works, and I am getting the correct data back now! 🙂 😄
11 replies
TTCTheo's Typesafe Cult
Created by admiralackbar411 on 6/7/2023 in #questions
How to get Next-Auth session to include custom properties
This is what the above returns
{ user: { name: null, email: '[email protected]', image: undefined } }
token: {"name":null,"email":"[email protected]","sub":"5","iat":1686223502,"exp":1688815502,"jti":"6394f114-c749-4e95-801e-a95c15cdb146"}
user: {"id":5,"username":"test","email":"[email protected]","password":"$2b$10$dbd/bBJ3VGUVVN8H494L1uoZImkGi1yiIzktpADHkSyffuLRAvcgq","name":null,"profileImage":null,"createdAt":"2023-06-08T11:24:44.708Z","updatedAt":"2023-06-08T11:24:44.708Z","role":"MEMBER"}
Session: {"user":{"name":null,"email":"[email protected]"},"expires":"2023-07-08T11:25:24.609Z"}
TokenSession: {"user":{"name":null,"email":"[email protected]"},"expires":"2023-07-08T11:25:24.609Z"}
token: {"name":null,"email":"[email protected]","sub":"5","iat":1686223524,"exp":1688815524,"jti":"1f883b4f-cd36-4a3e-bc77-9457a137b247"}
user: {"id":5,"username":"test","email":"[email protected]","password":"$2b$10$dbd/bBJ3VGUVVN8H494L1uoZImkGi1yiIzktpADHkSyffuLRAvcgq","name":null,"profileImage":null,"createdAt":"2023-06-08T11:24:44.708Z","updatedAt":"2023-06-08T11:24:44.708Z","role":"MEMBER"}
Session: {"user":{"name":null,"email":"[email protected]"},"expires":"2023-07-08T11:25:24.894Z"}
TokenSession: {"user":{"name":null,"email":"[email protected]"},"expires":"2023-07-08T11:25:24.894Z"}
{ user: { name: null, email: '[email protected]', image: undefined } }
token: {"name":null,"email":"[email protected]","sub":"5","iat":1686223502,"exp":1688815502,"jti":"6394f114-c749-4e95-801e-a95c15cdb146"}
user: {"id":5,"username":"test","email":"[email protected]","password":"$2b$10$dbd/bBJ3VGUVVN8H494L1uoZImkGi1yiIzktpADHkSyffuLRAvcgq","name":null,"profileImage":null,"createdAt":"2023-06-08T11:24:44.708Z","updatedAt":"2023-06-08T11:24:44.708Z","role":"MEMBER"}
Session: {"user":{"name":null,"email":"[email protected]"},"expires":"2023-07-08T11:25:24.609Z"}
TokenSession: {"user":{"name":null,"email":"[email protected]"},"expires":"2023-07-08T11:25:24.609Z"}
token: {"name":null,"email":"[email protected]","sub":"5","iat":1686223524,"exp":1688815524,"jti":"1f883b4f-cd36-4a3e-bc77-9457a137b247"}
user: {"id":5,"username":"test","email":"[email protected]","password":"$2b$10$dbd/bBJ3VGUVVN8H494L1uoZImkGi1yiIzktpADHkSyffuLRAvcgq","name":null,"profileImage":null,"createdAt":"2023-06-08T11:24:44.708Z","updatedAt":"2023-06-08T11:24:44.708Z","role":"MEMBER"}
Session: {"user":{"name":null,"email":"[email protected]"},"expires":"2023-07-08T11:25:24.894Z"}
TokenSession: {"user":{"name":null,"email":"[email protected]"},"expires":"2023-07-08T11:25:24.894Z"}
11 replies
TTCTheo's Typesafe Cult
Created by admiralackbar411 on 6/7/2023 in #questions
How to get Next-Auth session to include custom properties
I tried that repo and I still can't seem to get it to work - I have done a bit of logging and it's confusing me a bit now in terms of what I am getting in response, using that repo I don't seem to be able to get any of my extended data to show up, other than in some logging, but it doesn't get returned at all.
callbacks: {
async session({ token, session }) {
console.log(`Session: ${JSON.stringify(session)}`)
if (token) {
session.user.username = token.username;
session.user.profileImage = token.profileImage;
session.user.role = token.role;
session.user.id = token.id;
console.log(`TokenSession: ${JSON.stringify(session)}`)
}
return session;
},
async jwt({ token, user }) {
const dbUser = await prisma.user.findFirst({
where: {
email: token.email,
}
})

if (!dbUser) {
if (user) {
token.id = user?.id;
token.role = user?.role;
token.username = user?.username;
token.profileImage = user?.profileImage;
token.email = user?.email;
console.log(`UserTokenCheck: ${JSON.stringify(token)}`)
}

console.log(`NoDbUserToken: ${JSON.stringify(token)}`)
return token
}

console.log(`token: ${JSON.stringify(token)}`)
console.log(`user: ${JSON.stringify(dbUser)}`)

return token
}
}
callbacks: {
async session({ token, session }) {
console.log(`Session: ${JSON.stringify(session)}`)
if (token) {
session.user.username = token.username;
session.user.profileImage = token.profileImage;
session.user.role = token.role;
session.user.id = token.id;
console.log(`TokenSession: ${JSON.stringify(session)}`)
}
return session;
},
async jwt({ token, user }) {
const dbUser = await prisma.user.findFirst({
where: {
email: token.email,
}
})

if (!dbUser) {
if (user) {
token.id = user?.id;
token.role = user?.role;
token.username = user?.username;
token.profileImage = user?.profileImage;
token.email = user?.email;
console.log(`UserTokenCheck: ${JSON.stringify(token)}`)
}

console.log(`NoDbUserToken: ${JSON.stringify(token)}`)
return token
}

console.log(`token: ${JSON.stringify(token)}`)
console.log(`user: ${JSON.stringify(dbUser)}`)

return token
}
}
11 replies