Is there a way to get the additionalFields in the useSession hook?

I dont know if this has been asked before (couldnt find anything in the search, but is it possinle that if i add an additional field (call it role, like in the example) that i can see it in the useSession hook?
6 Replies
AbdallahGamal
AbdallahGamal4mo ago
yes add inferAdditionalFields plugin to the authClient
import { inferAdditionalFields } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import type { auth } from "./auth";

export const authClient = createAuthClient({
plugins: [
inferAdditionalFields<typeof auth>(),
],
});
import { inferAdditionalFields } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import type { auth } from "./auth";

export const authClient = createAuthClient({
plugins: [
inferAdditionalFields<typeof auth>(),
],
});
OakyLabs
OakyLabsOP4mo ago
this one is there in the type system, but not there in the runtime if i add that code it works in the type system, but when i look for it in the runtime the useSession hook does not return that value from the user
AbdallahGamal
AbdallahGamal4mo ago
that's not correct, they exists in the runtime. make sure you are on the latest version and did the database migration. if it doesn't work consider opening an issue on github.
OakyLabs
OakyLabsOP4mo ago
i got it working. it was the database migration all along :/ however i found a bug that i will raise an issue later today or tomrrow.. when using sqlite and adding additionalfields as boolean wirth a defaultValue of false, it was setting it to true weirdly enough
AbdallahGamal
AbdallahGamal4mo ago
check the migrations files and see if the filed is set to DEFAULT FALSE or not if its not present please post on github issues. consider marking this as done or solved
LightTab2
LightTab24mo ago
Session Management | Better Auth
Better Auth session management.

Did you find this page helpful?