snouzy
snouzy
BABetter Auth
Created by FrancyMak on 4/20/2025 in #help
useSession weird behaviour
Is it possible for you to provide a minimal reproduction example via github ? I'll be happy to help
16 replies
BABetter Auth
Created by FrancyMak on 4/20/2025 in #help
useSession weird behaviour
Does it work if you call useCurrentUser right in useNotificationsQuery ?
16 replies
BABetter Auth
Created by FrancyMak on 4/20/2025 in #help
useSession weird behaviour
Oh okay, so in the first place you cannot do session.data!.user.id because in the first render, session.data will not be defined, since it's client side it will have a little throttle (bcs of the cookie storage). So the error say that he's waiting for the user is right. Here's what i did :
export const useCurrentUser = () => {
const session = useSession();
const user = session.data?.user;

if (!user) {
return null;
}

return user;
};
export const useCurrentUser = () => {
const session = useSession();
const user = session.data?.user;

if (!user) {
return null;
}

return user;
};
export const useCurrentSession = () => {
const session = useSession();
const sessionData = session.data;

if (!sessionData) {
return null;
}

return sessionData;
};
export const useCurrentSession = () => {
const session = useSession();
const sessionData = session.data;

if (!sessionData) {
return null;
}

return sessionData;
};
16 replies
BABetter Auth
Created by FrancyMak on 4/20/2025 in #help
useSession weird behaviour
Hey, you are trying to access the useSession in a server context, which is a hook reserved to client side. What you should do, in a server context, is something like this :
// src/lib/auth.ts
export const auth = betterAuth({...});
// src/lib/auth.ts
export const auth = betterAuth({...});
// app/dashboard/layout.tsx
const headerStore = await headers();
const user = await auth.api.getSession({ headers: headerStore });
// app/dashboard/layout.tsx
const headerStore = await headers();
const user = await auth.api.getSession({ headers: headerStore });
If you really want to use useSession(), you should add "use client" in the top of your file
16 replies
BABetter Auth
Created by snouzy on 4/18/2025 in #help
Is it a possible to use a relation table as type for fields in additionalFields ? (beginner šŸ˜…)
Oups, that was for testing purposes. I just missed the fact that i needed customSessionClient instead of inferAdditionalFields Now everything is fully typed, even the relation with pages
export const authClient = createAuthClient({
/** The base URL of the server (optional if you're using the same domain) */
baseURL: getServerUrl(),
plugins: [customSessionClient<typeof auth>()],
});
export const authClient = createAuthClient({
/** The base URL of the server (optional if you're using the same domain) */
baseURL: getServerUrl(),
plugins: [customSessionClient<typeof auth>()],
});
What's your opinion about this ? āœ… Is it okay to include pages directly in the session šŸ¤” Or is it better to keep the session lightweight and fetch /me on the client side
4 replies