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
106 changes: 92 additions & 14 deletions src/cookies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe("createStorageFromOptions for createServerClient", () => {
return [];
},

setAll: async () => {
setAll: async (_cookies, _headers) => {
setAllCalled = true;
},
},
Expand All @@ -252,7 +252,7 @@ describe("createStorageFromOptions for createServerClient", () => {
return [];
},

setAll: async () => {
setAll: async (_cookies, _headers) => {
setAllCalled = true;
},
},
Expand Down Expand Up @@ -280,7 +280,7 @@ describe("createStorageFromOptions for createServerClient", () => {
return [];
},

setAll: async () => {},
setAll: async (_cookies, _headers) => {},
},
},
true,
Expand All @@ -307,7 +307,7 @@ describe("createStorageFromOptions for createServerClient", () => {
return [];
},

setAll: async () => {},
setAll: async (_cookies, _headers) => {},
},
},
true,
Expand All @@ -334,7 +334,7 @@ describe("createStorageFromOptions for createServerClient", () => {
return [];
},

setAll: async () => {},
setAll: async (_cookies, _headers) => {},
},
},
true,
Expand Down Expand Up @@ -377,7 +377,7 @@ describe("createStorageFromOptions for createServerClient", () => {
];
},

setAll: async () => {},
setAll: async (_cookies, _headers) => {},
},
},
true,
Expand Down Expand Up @@ -418,7 +418,7 @@ describe("createStorageFromOptions for createServerClient", () => {
];
},

setAll: async () => {},
setAll: async (_cookies, _headers) => {},
},
},
true,
Expand Down Expand Up @@ -577,7 +577,7 @@ describe("createStorageFromOptions for createBrowserClient", () => {
];
},

setAll: async () => {},
setAll: async (_cookies, _headers) => {},
},
},
false,
Expand Down Expand Up @@ -607,7 +607,7 @@ describe("createStorageFromOptions for createBrowserClient", () => {
return [];
},

setAll: async () => {
setAll: async (_cookies, _headers) => {
setAllCalls += 1;
},
},
Expand Down Expand Up @@ -640,7 +640,7 @@ describe("createStorageFromOptions for createBrowserClient", () => {
];
},

setAll: async () => {
setAll: async (_cookies, _headers) => {
setAllCalls += 1;
},
},
Expand Down Expand Up @@ -682,7 +682,7 @@ describe("createStorageFromOptions for createBrowserClient", () => {
];
},

setAll: async (setCookies) => {
setAll: async (setCookies, _headers) => {
setAllCalls.push(...setCookies);
},
},
Expand Down Expand Up @@ -753,7 +753,7 @@ describe("createStorageFromOptions for createBrowserClient", () => {
];
},

setAll: async (setCookies) => {
setAll: async (setCookies, _headers) => {
setAllCalls.push(...setCookies);
},
},
Expand Down Expand Up @@ -828,7 +828,7 @@ describe("createStorageFromOptions for createBrowserClient", () => {
];
},

setAll: async (setCookies) => {
setAll: async (setCookies, _headers) => {
setAllCalls.push(...setCookies);
},
},
Expand Down Expand Up @@ -903,7 +903,7 @@ describe("createStorageFromOptions for createBrowserClient", () => {
];
},

setAll: async (setCookies) => {
setAll: async (setCookies, _headers) => {
setAllCalls.push(...setCookies);
},
},
Expand Down Expand Up @@ -944,6 +944,84 @@ describe("createStorageFromOptions for createBrowserClient", () => {
});
});

describe("setAll arity enforcement", () => {
it("should throw when setAll accepts zero parameters (server)", () => {
expect(() => {
createStorageFromOptions(
{
cookieEncoding: "raw",
cookies: {
getAll: async () => [],
setAll: async () => {},
},
},
true,
);
}).toThrow(/must accept two parameters/);
});

it("should throw when setAll accepts one parameter (server)", () => {
expect(() => {
createStorageFromOptions(
{
cookieEncoding: "raw",
cookies: {
getAll: async () => [],
setAll: async (_cookies: any) => {},
},
},
true,
);
}).toThrow(/must accept two parameters/);
});

it("should throw when setAll accepts zero parameters (browser)", () => {
expect(() => {
createStorageFromOptions(
{
cookieEncoding: "raw",
cookies: {
getAll: async () => [],
setAll: async () => {},
},
},
false,
);
}).toThrow(/must accept two parameters/);
});

it("should not throw when setAll accepts two parameters", () => {
expect(() => {
createStorageFromOptions(
{
cookieEncoding: "raw",
cookies: {
getAll: async () => [],
setAll: async (_cookies: any, _headers: any) => {},
},
},
true,
);
}).not.toThrow();
});

it("should not throw for deprecated get/set/remove path", () => {
expect(() => {
createStorageFromOptions(
{
cookieEncoding: "raw",
cookies: {
get: async () => null,
set: async () => {},
remove: async () => {},
},
},
true,
);
}).not.toThrow();
});
});

describe("applyServerStorage", () => {
it("should call setAll with the correct cookies for a variety of changes to the storage state", async () => {
const setAllCalls: {
Expand Down
11 changes: 10 additions & 1 deletion src/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,16 @@ export function createStorageFromOptions(
getAll = async () => await cookies.getAll!();

if ("setAll" in cookies) {
setAll = cookies.setAll!;
const userSetAll = cookies.setAll!;
if (userSetAll.length < 2) {
throw new Error(
`@supabase/ssr: The setAll cookie method must accept two parameters: (cookies, headers). ` +
`Your function only accepts ${userSetAll.length}. The second parameter contains cache-control ` +
`headers that must be forwarded to the HTTP response to prevent CDNs from caching ` +
`authenticated responses. See https://supabase.com/docs/guides/auth/server-side/creating-a-client for examples.`,
);
}
setAll = userSetAll;
} else if (isServerClient) {
setAll = async () => {
console.warn(
Expand Down
20 changes: 10 additions & 10 deletions src/createServerClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("createServerClient", () => {
return [];
},

setAll(cookiesToSet) {
setAll(cookiesToSet, _headers) {
// no-op
},
},
Expand All @@ -28,7 +28,7 @@ describe("createServerClient", () => {
return [];
},

setAll(cookiesToSet) {
setAll(cookiesToSet, _headers) {
// no-op
},
},
Expand Down Expand Up @@ -63,7 +63,7 @@ describe("createServerClient", () => {
return [];
},

setAll(cookiesToSet) {
setAll(cookiesToSet, _headers) {
setAllCalls += 1;
setCookies.push(...cookiesToSet);
},
Expand Down Expand Up @@ -127,7 +127,7 @@ describe("createServerClient", () => {
];
},

setAll(cookiesToSet) {
setAll(cookiesToSet, _headers) {
setAllCalls += 1;
setCookies.push(...cookiesToSet);
},
Expand Down Expand Up @@ -221,7 +221,7 @@ describe("createServerClient", () => {
];
},

setAll(cookiesToSet) {
setAll(cookiesToSet, _headers) {
setAllCalls += 1;
setCookies.push(...cookiesToSet);
},
Expand Down Expand Up @@ -329,7 +329,7 @@ describe("createServerClient", () => {
];
},

setAll(cookiesToSet) {
setAll(cookiesToSet, _headers) {
setAllCalls += 1;
},
},
Expand Down Expand Up @@ -410,7 +410,7 @@ describe("createServerClient", () => {
];
},

setAll(cookiesToSet) {
setAll(cookiesToSet, _headers) {
setAllCalls += 1;
},
},
Expand Down Expand Up @@ -464,7 +464,7 @@ describe("createServerClient", () => {
},
];
},
setAll() {},
setAll(_cookiesToSet, _headers) {},
},

global: {
Expand Down Expand Up @@ -539,7 +539,7 @@ describe("createServerClient", () => {
];
},

setAll(cookiesToSet) {
setAll(cookiesToSet, _headers) {
setAllCalls += 1;
},
},
Expand Down Expand Up @@ -599,7 +599,7 @@ describe("createServerClient", () => {
getAll() {
return [];
},
setAll() {},
setAll(_cookiesToSet, _headers) {},
},
global: {
fetch: async () => {
Expand Down
Loading