BA
Better Auth•2w ago
skidy

getSession null

why useSession works but using getSession i get null
8 Replies
Ping
Ping•2w ago
Are you calling authClient or auth.api?
skidy
skidyOP•2w ago
authClient within layout.tsx
import { authClient } from "@/lib/auth-client";
import { Center, Container } from "@mantine/core";
import { redirect } from "next/navigation";

export default async function AuthLayout({
children,
}: {
children: React.ReactNode;
}) {
const { data: session } = await authClient.getSession();

console.log("session", session);
if (session) {
redirect("/");
}

return (
<Center w="100%" h="100%">
<Container w={420}>{children}</Container>
</Center>
);
}
import { authClient } from "@/lib/auth-client";
import { Center, Container } from "@mantine/core";
import { redirect } from "next/navigation";

export default async function AuthLayout({
children,
}: {
children: React.ReactNode;
}) {
const { data: session } = await authClient.getSession();

console.log("session", session);
if (session) {
redirect("/");
}

return (
<Center w="100%" h="100%">
<Container w={420}>{children}</Container>
</Center>
);
}
Ping
Ping•2w ago
Since that's a server component, you should call auth.api, make sure to include headers in the fetchOptions.
skidy
skidyOP•2w ago
oh i mistook it does this also work with routes?
Ping
Ping•2w ago
Yeah. Any code ran on the server should use auth.api. If code is ran on the client, use authClient.
rhitune
rhitune•2w ago
its server component not client
Felix
Felix•2w ago
So, what would be "best practice" if I don't have the auth.api in NextJS because, for example, I do my authentication centrally with Express? I have a similar case, and unfortunately, it only works on localhost... 😦 https://discord.com/channels/1288403910284935179/1359765737140523060

Did you find this page helpful?