Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Forward convexUrl in Next.js auth helpers
  • Loading branch information
JohnKesko committed May 2, 2026
commit ec0c2aaf57a792da41f1b41fa464c578955dc891
77 changes: 77 additions & 0 deletions src/nextjs/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

const fetchAction = vi.fn(async () => ({ ok: true }));
const fetchMutation = vi.fn(async () => ({ ok: true }));
const fetchQuery = vi.fn(async () => ({ ok: true }));
const preloadQuery = vi.fn(async () => ({ ok: true }));

vi.mock("convex/nextjs", () => ({
fetchAction,
fetchMutation,
fetchQuery,
preloadQuery,
}));

vi.mock("next/headers.js", () => ({
headers: vi.fn(async () => new Headers()),
}));

vi.mock("../utils/index.js", () => ({
getToken: vi.fn(async () => ({ isFresh: true, token: "test-token" })),
}));

describe("convexBetterAuthNextJs", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("passes convexUrl to authenticated Convex fetches", async () => {
const { convexBetterAuthNextJs } = await import("./index.js");
const query = "users:getCurrentUser" as never;
const mutation = "users:createOrUpdateUserFromAuth" as never;
const action = "admin:sendDirectSupportEmail" as never;

const auth = convexBetterAuthNextJs({
convexUrl: "https://example-deployment.convex.cloud",
convexSiteUrl: "https://example-deployment.convex.site",
});

await auth.preloadAuthQuery(query, { userId: "user_123" } as never);
await auth.fetchAuthQuery(query, { userId: "user_123" } as never);
await auth.fetchAuthMutation(mutation, { userId: "user_123" } as never);
await auth.fetchAuthAction(action, { userId: "user_123" } as never);

expect(preloadQuery).toHaveBeenCalledWith(
query,
{ userId: "user_123" },
{
token: "test-token",
url: "https://example-deployment.convex.cloud",
}
);
expect(fetchQuery).toHaveBeenCalledWith(
query,
{ userId: "user_123" },
{
token: "test-token",
url: "https://example-deployment.convex.cloud",
}
);
expect(fetchMutation).toHaveBeenCalledWith(
mutation,
{ userId: "user_123" },
{
token: "test-token",
url: "https://example-deployment.convex.cloud",
}
);
expect(fetchAction).toHaveBeenCalledWith(
action,
{ userId: "user_123" },
{
token: "test-token",
url: "https://example-deployment.convex.cloud",
}
);
});
});
15 changes: 8 additions & 7 deletions src/nextjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ type OptionalArgs<FuncRef extends FunctionReference<any, any>> =

const getArgsAndOptions = <FuncRef extends FunctionReference<any, any>>(
args: OptionalArgs<FuncRef>,
token?: string
): ArgsAndOptions<FuncRef, { token?: string }> => {
return [args[0], { token }];
token: string | undefined,
convexUrl: string
): ArgsAndOptions<FuncRef, { token?: string; url: string }> => {
return [args[0], { token, url: convexUrl }];
};

export const convexBetterAuthNextJs = (
Expand Down Expand Up @@ -123,7 +124,7 @@ export const convexBetterAuthNextJs = (
...args: OptionalArgs<Query>
): Promise<Preloaded<Query>> => {
return callWithToken((token?: string) => {
const argsAndOptions = getArgsAndOptions(args, token);
const argsAndOptions = getArgsAndOptions(args, token, opts.convexUrl);
return preloadQuery(query, ...argsAndOptions);
});
},
Expand All @@ -132,7 +133,7 @@ export const convexBetterAuthNextJs = (
...args: OptionalArgs<Query>
): Promise<FunctionReturnType<Query>> => {
return callWithToken((token?: string) => {
const argsAndOptions = getArgsAndOptions(args, token);
const argsAndOptions = getArgsAndOptions(args, token, opts.convexUrl);
return fetchQuery(query, ...argsAndOptions);
});
},
Expand All @@ -141,7 +142,7 @@ export const convexBetterAuthNextJs = (
...args: OptionalArgs<Mutation>
): Promise<FunctionReturnType<Mutation>> => {
return callWithToken((token?: string) => {
const argsAndOptions = getArgsAndOptions(args, token);
const argsAndOptions = getArgsAndOptions(args, token, opts.convexUrl);
return fetchMutation(mutation, ...argsAndOptions);
});
},
Expand All @@ -150,7 +151,7 @@ export const convexBetterAuthNextJs = (
...args: OptionalArgs<Action>
): Promise<FunctionReturnType<Action>> => {
return callWithToken((token?: string) => {
const argsAndOptions = getArgsAndOptions(args, token);
const argsAndOptions = getArgsAndOptions(args, token, opts.convexUrl);
return fetchAction(action, ...argsAndOptions);
});
},
Expand Down
Loading