how to update the session

So I am quite new to better auth; earlier, next auth used to be my go-to solution, but it's getting rough. I am struggling to understand the designs of better auth. When I use getSession, the session is an object of session and user.

const {data: session} = authClient.useSession();
?^ session: {
session: Session;
user: User:
}

const {data: session} = authClient.useSession();
?^ session: {
session: Session;
user: User:
}
Now I want to extend the session object. Let's say I want to add orgSlug to the session. Where should I add it? On session.session or session.user?
According to the docs, Customizing Session Response added a "newFiled" to session.user. Where should I add this, and more importantly, how?
23 Replies
Prashant
PrashantOP•3w ago
So, if I go according to the docs, let's add an orgSlug on session.user If I go according to the docs workaround for type interferences in custom session I am loosing all my types customSession
Prashant
PrashantOP•3w ago
this is what the docs suggested
No description
Prashant
PrashantOP•3w ago
this is my code ss, the same goes with session. Both session and user are inferring as unknown
No description
Prashant
PrashantOP•3w ago
if I remove options, it behaves normally
No description
Prashant
PrashantOP•3w ago
I am in sooo much pain 😢 back to customizing the session wait a sec, I am getting pissed rn there are two different ways to infer custom fields on auth-client??? whyyyyy
Prashant
PrashantOP•3w ago
No description
No description
Prashant
PrashantOP•3w ago
the first one is under typescript and he second one is under customizing session response
Ping
Ping•3w ago
May I see where you defined options? Most cases, the reason why the user and session become unknown is because the auth config inferred the plugins array as empty []. But you may have other stuff defined in options, 🤔 Both of them act differently, the first ss is to infer any additionalFields or mapped fields you may have defined in your auth config. The second is purely for mapping the session & user by your customSession plugin
Prashant
PrashantOP•3w ago
felt overwhelmed hey @Ping I want help on session update. How do I update it btw which to use?
Ping
Ping•3w ago
I assume you mean you want the orgSlug in the session. Here's an example
No description
Ping
Ping•3w ago
In this case where you're just wanting the customSession inference, just use the customSessionClient.
Prashant
PrashantOP•3w ago
lemme try this
Prashant
PrashantOP•3w ago
No description
Prashant
PrashantOP•3w ago
is this how it works?
Prashant
PrashantOP•3w ago
options for aut.ts
No description
Prashant
PrashantOP•3w ago
this is what I am experiencing rn
No description
No description
Prashant
PrashantOP•3w ago
the first ss is of the type error I am getting when I am trying to use authclient.udpateUser to update the org slug the second one are the logs,
Ping
Ping•3w ago
You can't set that via updateUser. You need to use your ORM to set this
Prashant
PrashantOP•3w ago
so in better-auth you can't update the session ?
Ping
Ping•3w ago
Yes. Just use your ORM, that's updating the session.
Prashant
PrashantOP•3w ago
😢 how to do it via orm probaly like I am using drizzle with orm i am update it in db, but how to update it on client? wait a sec, just found something
Prashant
PrashantOP•3w ago
I can confirm that this works
No description
Prashant
PrashantOP•3w ago
i am able to update the session @Ping this works for me :)) so what i did was I updated the session on db refetch on client

Did you find this page helpful?