Skip to content

Commit 270a0af

Browse files
ikoamufossamagna
andauthored
fix: ensure cookie names are encoded for js-cookie compatibility (#92)
* fix: ensure cookie names are encoded for js-cookie compatibility * chore: add changeset to ensure cookie names are encoded for js-cookie compatibility --------- Co-authored-by: MURAKAMI Masahiko <[email protected]>
1 parent 64a85db commit 270a0af

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

.changeset/quiet-clocks-open.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"amplify-adapter-react-router": patch
3+
---
4+
5+
fix: ensure cookie names are encoded for js-cookie compatibility

packages/amplify-adapter-react-router/src/adapter.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ import {
1414
import { parseAmplifyConfig } from "aws-amplify/utils";
1515
import cookie from "cookie";
1616

17+
/**
18+
* Ensures the cookie names are encoded in order to look up the cookie store
19+
* that is manipulated by js-cookie on the client side.
20+
*
21+
* see: https://github.com/aws-amplify/amplify-js/blob/main/packages/adapter-nextjs/src/utils/cookie/ensureEncodedForJSCookie.ts
22+
*/
23+
function ensureEncodedForJSCookie(name: string) {
24+
return encodeURIComponent(name).replace(
25+
/%(2[346B]|5E|60|7C)/g,
26+
decodeURIComponent,
27+
);
28+
}
29+
1730
export type ReactRouterServerContext = {
1831
request: Request;
1932
responseHeaders: Headers;
@@ -72,24 +85,30 @@ export function createServerRunner({
7285
name,
7386
value,
7487
})),
75-
get: (name: string): CookieStorage.Cookie | undefined => ({
76-
name,
77-
value: cookies[name],
78-
}),
88+
get: (name: string): CookieStorage.Cookie | undefined => {
89+
const encodedName = ensureEncodedForJSCookie(name);
90+
return { name: encodedName, value: cookies[encodedName] };
91+
},
7992
set: (
8093
name: string,
8194
value: string,
8295
options?: CookieStorage.SetCookieOptions,
8396
): void => {
8497
responseHeaders.append(
8598
"Set-Cookie",
86-
cookie.serialize(name, value, options),
99+
cookie.serialize(
100+
ensureEncodedForJSCookie(name),
101+
value,
102+
options,
103+
),
87104
);
88105
},
89106
delete: (name: string): void => {
90107
responseHeaders.append(
91108
"Set-Cookie",
92-
cookie.serialize(name, "", { expires: new Date(0) }),
109+
cookie.serialize(ensureEncodedForJSCookie(name), "", {
110+
expires: new Date(0),
111+
}),
93112
);
94113
},
95114
});

0 commit comments

Comments
 (0)