Better AuthBA
Better Auth9mo ago
vz

useSession returns null

Anyone knows why useSession returns null?

1) no cookie prefix or any cookie name customization
2) i can see cookie logs(in middleware)
export async function middleware(request: NextRequest) {
  // In Next.js middleware, it's recommended to only check for the existence of a session cookie to handle redirection. To avoid blocking requests by making API or database calls.
  const sessionCookie = getSessionCookie(request, {
  });

  logger.debug("🍪 Session cookie:", sessionCookie);
// etc....


3)but i get null from useSession
const { data: authClientSession } = authClient.useSession();
 console.log(authClientSession) // returns null


4) auth client looks like this
import {
  adminClient,
  emailOTPClient,
  organizationClient,
} from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";

export const authClient = createAuthClient({
  baseURL: "http://localhost:3000",
  plugins: [emailOTPClient(), organizationClient(), adminClient()],
});
export const { signIn, signUp, useSession, signOut } = authClient;
image.png
Solution
thanks! i realize that when you render something depdening on the session data and it's null at the begining, it just stops there and wont continue
Was this page helpful?