pass discord ID to session

I am trying to pass a users Discord ID through to the user object in session. I am aware of mapProfileToUser but not of how to use it to accomplish adding it to the session. I know how to add it custom fields to the session, just not how to get the profile object from socialProviders: { discord to custom session
2 Replies
avvo
avvo3w ago
why do you need the discord id in the session object just add it to request context no?
Jacob
Jacob3w ago
Ok my code for mapping it:
mapProfileToUser: (profile) => {
if (!profile.verified) {
throw new Error("Discord email is not verified.");
}

return {
...profile,
discordUsername: profile.username,
image: profile.avatar
? `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png`
: undefined,
};
},
mapProfileToUser: (profile) => {
if (!profile.verified) {
throw new Error("Discord email is not verified.");
}

return {
...profile,
discordUsername: profile.username,
image: profile.avatar
? `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png`
: undefined,
};
},
And there is example code for the organizations plugin that shows how to pass data in the session before its created so use that sample:
databaseHooks: {
session: {
create: {
before: async (session: { userId: string }) => {
try {
const organization = await getActiveOrganization(session.userId);
return {
data: {
...session,
activeOrganizationId: organization.id,
},
};
} catch (error) {
console.error("Error getting active organization:", error);
return { data: session };
}
},
},
},
databaseHooks: {
session: {
create: {
before: async (session: { userId: string }) => {
try {
const organization = await getActiveOrganization(session.userId);
return {
data: {
...session,
activeOrganizationId: organization.id,
},
};
} catch (error) {
console.error("Error getting active organization:", error);
return { data: session };
}
},
},
},
But I am interested to see how others would do this.

Did you find this page helpful?