diff --git a/packages/better-auth/src/api/index.ts b/packages/better-auth/src/api/index.ts index 163dba81bc..9dc88950d0 100644 --- a/packages/better-auth/src/api/index.ts +++ b/packages/better-auth/src/api/index.ts @@ -172,9 +172,11 @@ export const router = ( _options: Option, ) => { const { api, middlewares } = getEndpoints(ctx, _options); + const basePath = new URL(ctx.baseURL).pathname; + return createRouter(api, { extraContext: ctx, - basePath: new URL(ctx.baseURL).pathname, + basePath, routerMiddleware: [ { path: "/**", @@ -182,31 +184,6 @@ export const router = ( }, ...middlewares, ], - // /** - // * this is to remove any sensitive data from the response - // */ - async transformResponse(res) { - let body: Record = {}; - try { - body = await res.json(); - } catch (e) { - return res; - } - if (body?.user) { - body.user = parseUser(ctx.options, body.user); - } - if (body?.session) { - body.session = parseSession(ctx.options, body.session); - } - if (body?.account) { - body.account = parseAccount(ctx.options, body.account); - } - return new Response(body ? JSON.stringify(body) : null, { - headers: res.headers, - status: res.status, - statusText: res.statusText, - }); - }, onError(e) { console.log(e); }, diff --git a/packages/better-auth/src/api/routes/credential.test.ts b/packages/better-auth/src/api/routes/credential.test.ts index f1f43551ab..c41c246b6d 100644 --- a/packages/better-auth/src/api/routes/credential.test.ts +++ b/packages/better-auth/src/api/routes/credential.test.ts @@ -1,7 +1,9 @@ -import { parseCookies } from "oslo/cookie"; import { describe, expect, it } from "vitest"; import { getTestInstance } from "../../test-utils/test-instance"; +import { parseSetCookieHeader } from "../../utils/cookies"; + const { auth, client } = await getTestInstance(); + const testCredential1 = { email: "test@test.com", password: "test123456", @@ -129,12 +131,28 @@ describe("sign-in credential", async () => { onResponse(context) { const headers = context.response.headers; const cookies = headers.get("set-cookie") || ""; - const parsedCookie = parseCookies(cookies); + const parsedCookie = parseSetCookieHeader(cookies); + const sessionCookie = parsedCookie.get("better-auth.session_token"); + expect(sessionCookie).toMatchObject({ + value: expect.any(String), + path: "/", + httponly: true, + samesite: "Lax", + }); + const dontRememberCookie = parsedCookie.get( + "better-auth.dont_remember", + ); + expect(dontRememberCookie).toMatchObject({ + value: expect.any(String), + path: "/", + httponly: true, + samesite: "Lax", + }); }, }, }); expect( new Date(res.data?.session.expiresAt || "").getTime() - Date.now(), - ).toBeLessThan(1000 * 60 * 60); + ).toBeLessThan(1000 * 60 * 60 * 24); }); }); diff --git a/packages/better-auth/src/api/routes/session.test.ts b/packages/better-auth/src/api/routes/session.test.ts new file mode 100644 index 0000000000..fcd5811817 --- /dev/null +++ b/packages/better-auth/src/api/routes/session.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from "vitest"; +import { getTestInstance } from "../../test-utils/test-instance"; +import { parseSetCookieHeader } from "../../utils/cookies"; + +const { auth, client, createTestUser } = await getTestInstance(); + +describe("session", async () => { + it("should set cookies correctly", async () => { + const testUser = await createTestUser(); + await client.signIn.email({ + email: testUser.email, + password: testUser.password, + options: { + onSuccess(context) { + const header = context.response.headers.get("set-cookie"); + const cookies = parseSetCookieHeader(header || ""); + expect(cookies.get("better-auth.session_token")).toMatchObject({ + value: expect.any(String), + "max-age": (60 * 60 * 24 * 7).toString(), + path: "/", + httponly: true, + samesite: "Lax", + }); + }, + }, + }); + }); +}); diff --git a/packages/better-auth/src/client/proxy.ts b/packages/better-auth/src/client/proxy.ts index 7c324e05c4..1a08cc4b7b 100644 --- a/packages/better-auth/src/client/proxy.ts +++ b/packages/better-auth/src/client/proxy.ts @@ -78,6 +78,7 @@ export function createDynamicPathProxy>( query: query, method, async onSuccess(context) { + await options?.onSuccess?.(context); /** * We trigger listeners */ @@ -85,11 +86,8 @@ export function createDynamicPathProxy>( if (!matches) return; const signal = atoms[matches.signal]; if (!signal) return; - setTimeout(() => { - //@ts-expect-error - signal.set(!signal.get()); - }, 0); - await options?.onSuccess?.(context); + //@ts-expect-error + signal.set(!signal.get()); }, }); }, diff --git a/packages/better-auth/src/test-utils/test-instance.ts b/packages/better-auth/src/test-utils/test-instance.ts index de61427f71..c3cba10e15 100644 --- a/packages/better-auth/src/test-utils/test-instance.ts +++ b/packages/better-auth/src/test-utils/test-instance.ts @@ -72,12 +72,12 @@ export async function getTestInstance>( const req = new Request(url.toString(), init); return auth.handler(req); }, - baseURL: options?.baseURL || "http://localhost:" + (port || 3000), + baseURL: + options?.baseURL || "http://localhost:" + (port || 3000) + "/api/auth", }, }); return { auth, - client, createTestUser, }; diff --git a/packages/better-auth/src/utils/cookies.ts b/packages/better-auth/src/utils/cookies.ts index 450cc13b28..c1ceb52656 100644 --- a/packages/better-auth/src/utils/cookies.ts +++ b/packages/better-auth/src/utils/cookies.ts @@ -138,3 +138,35 @@ export function deleteSessionCookie(ctx: GenericEndpointContext) { maxAge: 0, }); } + +type CookieAttributes = { + value: string; + [key: string]: string | boolean; +}; + +export function parseSetCookieHeader( + header: string, +): Map { + const cookieMap = new Map(); + + // Split the header into individual cookies + const cookies = header.split(", "); + + // biome-ignore lint/complexity/noForEach: + cookies.forEach((cookie) => { + const [nameValue, ...attributes] = cookie.split("; "); + const [name, value] = nameValue.split("="); + + const cookieObj: CookieAttributes = { value }; + + // biome-ignore lint/complexity/noForEach: + attributes.forEach((attr) => { + const [attrName, attrValue] = attr.split("="); + cookieObj[attrName.toLowerCase()] = attrValue || true; + }); + + cookieMap.set(name, cookieObj); + }); + + return cookieMap; +}