Next-Auth Access to custom data in sessions database

Hi All, I implemented a new column into the "sessions" table called "selectedClientID". Users will have access to multiple clients and I want the session to keep track of what client they have selected. In my current implementation, I can't seem to retrieve the "selectedClientID" in the sessions table. This is the current code:
callbacks: {
async session({ session, trigger, newSession, user, token }) {

session.user = {
...session.user,
role: user.role,
};
console.log("auth.ts: ", session);

if (trigger === "update" && newSession?.selectedClientID) {
session.selectedClientID = newSession.selectedClientID
}

return session;
},
},
callbacks: {
async session({ session, trigger, newSession, user, token }) {

session.user = {
...session.user,
role: user.role,
};
console.log("auth.ts: ", session);

if (trigger === "update" && newSession?.selectedClientID) {
session.selectedClientID = newSession.selectedClientID
}

return session;
},
},
In the above, ideally, the variable session would return the selected client and so if the trigger=== "update" and there is a new selectedClientID in "newSession", it would trigger an update and persist it to the database. I am unsure if I am looking at the problem the right way, so any help would be greatly appreciated. Many thanks!
9 Replies
Keef
Keef2y ago
Selecting clients is that similar to how some saas use organizations/teams? If that’s the case I’d probably not tie this mutable bit to the session
thal9201
thal9201OP2y ago
It is going to be B2B but for internal coworkers, they have access to multiple clients. I want them to be able to switch between clients
Keef
Keef2y ago
Usually if I’m extending users I’m using the User table with next auth and just manipulating it like any other model in my db I’m not familiar with the route you are doing so I can’t help much other than offer a different way and ask why you are doing it this way If it shouldn’t persist forever you should just keep it on the app in some kind of state If you want a default just stick to a property on the users table
thal9201
thal9201OP2y ago
Yea - I’ve done it via context before but issue is that every time a user refreshes a page, it’ll then forget about the “selectedClient” I can try to do it the user table way - it may be close enough to mimic the behavior I’d need for now
Keef
Keef2y ago
If you do the table it should solve the refresh too I’ve done it that way in the past
thal9201
thal9201OP2y ago
The issue I anticipate is if an internal user has multiple sessions (I.e. different devices) But I think that can be smth down the line
Keef
Keef2y ago
I think it’s a non issue I’d just keep it simple for now But I’m hesitant cause I over think some solutions before actual problems arise Even in that situation the selectedClient on the newest device is the correct one with the old ones being stale and should be revalidated So it’ll still work out
thal9201
thal9201OP2y ago
Kk I’ll try that implementation out then - thanks Keef! Will update here if I run into any further issues
Keef
Keef2y ago
peepoAing
Want results from more Discord servers?
Add your server