Skip to content

Commit 4f59826

Browse files
committed
adding global cookie override
1 parent 005c1c0 commit 4f59826

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@ export default function App() {
144144

145145
![react-toastify](./assets/sonner.gif)
146146

147+
## Overriding cookie options
148+
149+
You can override the default cookie options by passing in your own options via the `setToastCookieOptions` function.
150+
151+
```tsx
152+
import { setToastCookieOptions } from "remix-toast";
153+
154+
setToastCookieOptions({
155+
secrets:
156+
process.env.NODE_ENV === "production"
157+
? [process.env.SESSION_SECRET]
158+
: ["secret"]
159+
});
160+
```
161+
147162
## Creating utility functions with custom sessions
148163

149164
`createToastUtilsWithCustomSession` is a function that allows you to create a custom session for your toasts. This is useful if you want to have different types of toasts for different parts of your app.

src/index.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,42 @@ import {
44
redirect,
55
json,
66
SessionStorage,
7+
SessionIdStorageStrategy,
78
} from "@remix-run/server-runtime";
89
import { FlashSessionValues, ToastMessage, flashSessionValuesSchema } from "./schema";
910
import { sign, unsign } from "./crypto";
1011

1112
const FLASH_SESSION = "flash";
1213
const createCookie = createCookieFactory({ sign, unsign });
14+
type ToastCookieOptions = Partial<SessionIdStorageStrategy["cookie"]>;
15+
16+
const toastCookieOptions = {
17+
name: "toast-session",
18+
sameSite: "lax",
19+
path: "/",
20+
httpOnly: true,
21+
secrets: ["s3Cr3t"],
22+
} satisfies ToastCookieOptions;
1323

1424
const sessionStorage = createCookieSessionStorageFactory(createCookie)({
15-
cookie: {
16-
name: "toast-session",
17-
sameSite: "lax",
18-
path: "/",
19-
httpOnly: true,
20-
secrets: ["s3Cr3t"],
21-
},
25+
cookie: toastCookieOptions,
2226
});
2327

28+
/**
29+
* Sets the cookie options to be used for the toast cookie
30+
*
31+
* @param options Cookie options to be used for the toast cookie
32+
*/
33+
export function setToastCookieOptions(options: ToastCookieOptions) {
34+
Object.assign(toastCookieOptions, options);
35+
Object.assign(
36+
sessionStorage,
37+
createCookieSessionStorageFactory(createCookie)({
38+
cookie: toastCookieOptions,
39+
}),
40+
);
41+
}
42+
2443
function getSessionFromRequest(request: Request, customSession?: SessionStorage) {
2544
const cookie = request.headers.get("Cookie");
2645
const sessionToUse = customSession ? customSession : sessionStorage;
@@ -113,7 +132,7 @@ export async function getToast(
113132
return { toast, headers };
114133
}
115134

116-
export type { ToastMessage };
135+
export type { ToastMessage, ToastCookieOptions };
117136

118137
/**
119138
* Helper method used to initialize the whole library using a custom session. Returns all the utilities enhanced with the custom session

0 commit comments

Comments
 (0)