Auth Client not sending cookies to Server

I'm using authClient.$fetch in a project to make requests to my server (Elysia + Better Auth). However, I noticed that the server is not able to identify the user, because $fetch is not sending the cookies in the request. I tested adding the cookies manually to the request, and it worked correctly. My question is: shouldn't authClient.$fetch include cookies automatically? If that’s not the expected behavior, what is the advantage of using it instead of the standard fetch?
// auth-client.js
import { adminClient, organizationClient } from "better-auth/client/plugins";
import { nextCookies } from "better-auth/next-js";
import { createAuthClient } from "better-auth/react";

export const authClient = createAuthClient({
baseURL: "http://localhost:3005",
plugins: [
adminClient(),
organizationClient(),
nextCookies(),
]
})

// api-fetch.ts
import { cookies } from "next/headers"
import { authClient } from "./auth-client"

export default async function apiFetch(url: string) {
const cookieStore = await cookies()
const cookieHeader = Array.from(cookieStore.getAll())
.map(({ name, value }) => `${name}=${value}`)
.join("; ");

const resp = authClient.$fetch(url, {
headers: {
// manual cookie included
Cookie: cookieHeader,
},
})
return resp
}
// auth-client.js
import { adminClient, organizationClient } from "better-auth/client/plugins";
import { nextCookies } from "better-auth/next-js";
import { createAuthClient } from "better-auth/react";

export const authClient = createAuthClient({
baseURL: "http://localhost:3005",
plugins: [
adminClient(),
organizationClient(),
nextCookies(),
]
})

// api-fetch.ts
import { cookies } from "next/headers"
import { authClient } from "./auth-client"

export default async function apiFetch(url: string) {
const cookieStore = await cookies()
const cookieHeader = Array.from(cookieStore.getAll())
.map(({ name, value }) => `${name}=${value}`)
.join("; ");

const resp = authClient.$fetch(url, {
headers: {
// manual cookie included
Cookie: cookieHeader,
},
})
return resp
}
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?