Cannot use useSession hook
Hello, I use Better Auth with Next.js. I use the Discord provider and it works but when I want to retrieve the user's session, my IDE tells me that
My file
I run on
Thanks for help!
authClient.useSessionauthClient.useSession is not callable and I got this in my browser's console:GET http://localhost:3000/api/auth/use-session 404 (Not Found)GET http://localhost:3000/api/auth/use-session 404 (Not Found).My file
src/app/api/auth/[...all]/route.tssrc/app/api/auth/[...all]/route.ts looks like this:import { auth } from "@/auth";
import { toNextJsHandler } from "better-auth/next-js";
export const { POST, GET } = toNextJsHandler(auth);import { auth } from "@/auth";
import { toNextJsHandler } from "better-auth/next-js";
export const { POST, GET } = toNextJsHandler(auth);src/app/page.tsxsrc/app/page.tsx"use client"
import { authClient } from "@/auth-client";
import { Button } from "@/components/ui/button";
export default function Home() {
const { data: session } = authClient.useSession();
return (
<div>
<Button
onClick={(): void => {
authClient.signIn.social({
provider: "discord"
})
}}
>
Se connecter
</Button>
<div>
{JSON.stringify(session, null, 2)}
</div>
</div>
);
}"use client"
import { authClient } from "@/auth-client";
import { Button } from "@/components/ui/button";
export default function Home() {
const { data: session } = authClient.useSession();
return (
<div>
<Button
onClick={(): void => {
authClient.signIn.social({
provider: "discord"
})
}}
>
Se connecter
</Button>
<div>
{JSON.stringify(session, null, 2)}
</div>
</div>
);
}/src/auth.ts/src/auth.tsimport { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "@/db";
import * as schema from "@/db/schema";
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...schema,
user: schema.user,
},
}),
socialProviders: {
discord: {
clientId: process.env.DISCORD_CLIENT_ID as string,
clientSecret: process.env.DISCORD_CLIENT_SECRET as string,
},
},
});import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "@/db";
import * as schema from "@/db/schema";
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...schema,
user: schema.user,
},
}),
socialProviders: {
discord: {
clientId: process.env.DISCORD_CLIENT_ID as string,
clientSecret: process.env.DISCORD_CLIENT_SECRET as string,
},
},
});src/auth-client.tssrc/auth-client.tsimport { createAuthClient } from "better-auth/client"
export const authClient = createAuthClient({
baseURL: "http://localhost:3000"
})import { createAuthClient } from "better-auth/client"
export const authClient = createAuthClient({
baseURL: "http://localhost:3000"
})I run on
localhost:3000localhost:3000Thanks for help!