mirror of
https://github.com/better-auth/better-auth.git
synced 2026-10-25 10:13:54 -05:00
test: session and signin
This commit is contained in:
@@ -172,9 +172,11 @@ export const router = <C extends AuthContext, Option extends BetterAuthOptions>(
|
||||
_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 = <C extends AuthContext, Option extends BetterAuthOptions>(
|
||||
},
|
||||
...middlewares,
|
||||
],
|
||||
// /**
|
||||
// * this is to remove any sensitive data from the response
|
||||
// */
|
||||
async transformResponse(res) {
|
||||
let body: Record<string, any> = {};
|
||||
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);
|
||||
},
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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",
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -78,6 +78,7 @@ export function createDynamicPathProxy<T extends Record<string, any>>(
|
||||
query: query,
|
||||
method,
|
||||
async onSuccess(context) {
|
||||
await options?.onSuccess?.(context);
|
||||
/**
|
||||
* We trigger listeners
|
||||
*/
|
||||
@@ -85,11 +86,8 @@ export function createDynamicPathProxy<T extends Record<string, any>>(
|
||||
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());
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
@@ -72,12 +72,12 @@ export async function getTestInstance<O extends Partial<BetterAuthOptions>>(
|
||||
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,
|
||||
};
|
||||
|
||||
@@ -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<string, CookieAttributes> {
|
||||
const cookieMap = new Map<string, CookieAttributes>();
|
||||
|
||||
// Split the header into individual cookies
|
||||
const cookies = header.split(", ");
|
||||
|
||||
// biome-ignore lint/complexity/noForEach: <explanation>
|
||||
cookies.forEach((cookie) => {
|
||||
const [nameValue, ...attributes] = cookie.split("; ");
|
||||
const [name, value] = nameValue.split("=");
|
||||
|
||||
const cookieObj: CookieAttributes = { value };
|
||||
|
||||
// biome-ignore lint/complexity/noForEach: <explanation>
|
||||
attributes.forEach((attr) => {
|
||||
const [attrName, attrValue] = attr.split("=");
|
||||
cookieObj[attrName.toLowerCase()] = attrValue || true;
|
||||
});
|
||||
|
||||
cookieMap.set(name, cookieObj);
|
||||
});
|
||||
|
||||
return cookieMap;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user