Skip to content

Commit 4b1d17f

Browse files
authored
Allow configuring session storage on callback route (#64)
1 parent b4961bf commit 4b1d17f

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,75 @@ export const loader = (args: LoaderFunctionArgs) =>
272272
{ debug: true },
273273
);
274274
```
275+
276+
## Customizing Session Storage
277+
278+
By default, AuthKit for Remix uses cookie-based session storage with these settings:
279+
280+
```typescript
281+
{
282+
name: "wos-session", // Default or WORKOS_COOKIE_NAME if set
283+
path: "/",
284+
httpOnly: true,
285+
secure: true, // When redirect URI uses HTTPS
286+
sameSite: "lax",
287+
maxAge: 34560000, // 400 days (configurable via WORKOS_COOKIE_MAX_AGE)
288+
secrets: [/* your cookie password, configurable via WORKOS_COOKIE_PASSWORD */],
289+
}
290+
```
291+
292+
### Custom Session Storage
293+
294+
You can provide your own session storage implementation to both `authkitLoader` and `authLoader`:
295+
296+
```typescript
297+
import { createMemorySessionStorage } from "@remix-run/node";
298+
import { authkitLoader, authLoader } from "@workos-inc/authkit-remix";
299+
300+
// Create memory-based session storage
301+
const memoryStorage = createMemorySessionStorage({
302+
cookie: {
303+
name: "auth-session",
304+
secrets: ["test-secret"],
305+
sameSite: "lax",
306+
path: "/",
307+
httpOnly: true,
308+
secure: false, // Use false for testing
309+
maxAge: 60 * 60 * 24 // 1 day
310+
}
311+
});
312+
313+
// In your root loader
314+
export const loader = (args) => authkitLoader(args, {
315+
storage: memoryStorage,
316+
cookie: { name: "auth-session" }
317+
});
318+
319+
// In your callback route
320+
export const loader = authLoader({
321+
storage: memoryStorage,
322+
cookie: { name: "auth-session" }
323+
});
324+
```
325+
326+
For code reuse and consistency, consider using a shared function:
327+
328+
```typescript
329+
// app/lib/session.ts
330+
export function getAuthStorage() {
331+
const storage = createCookieSessionStorage({/* config */});
332+
return { storage, cookie: { name: "my-custom-session" } };
333+
}
334+
335+
// Then in your routes
336+
import { getAuthStorage } from "~/lib/session";
337+
export const loader = (args) => authkitLoader(args, {
338+
...getAuthStorage(),
339+
// Other options...
340+
});
341+
```
342+
343+
> [!NOTE]
344+
>When deploying to serverless environments like AWS Lambda, ensure you pass the same storage configuration to both your main routes and the callback route to handle cold starts properly.
345+
346+
AuthKit works with any session storage that implements Remix's `SessionStorage` interface, including Redis-based or database-backed implementations.

src/authkit-callback-route.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { LoaderFunctionArgs, data, redirect } from '@remix-run/node';
22
import { getConfig } from './config.js';
33
import { HandleAuthOptions } from './interfaces.js';
44
import { encryptSession } from './session.js';
5-
import { getSessionStorage } from './sessionStorage.js';
5+
import { configureSessionStorage } from './sessionStorage.js';
66
import { getWorkOS } from './workos.js';
77

88
export function authLoader(options: HandleAuthOptions = {}) {
99
return async function loader({ request }: LoaderFunctionArgs) {
10-
const { getSession, commitSession, cookieName } = await getSessionStorage();
11-
const { returnPathname: returnPathnameOption = '/', onSuccess } = options;
10+
const { storage, cookie, returnPathname: returnPathnameOption = '/', onSuccess } = options;
11+
const cookieName = cookie?.name ?? getConfig('cookieName');
12+
const { getSession, commitSession } = await configureSessionStorage({ storage, cookieName });
1213

1314
const url = new URL(request.url);
1415

src/interfaces.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ import type { OauthTokens, User } from '@workos-inc/node';
33

44
export type DataWithResponseInit<T> = ReturnType<typeof data<T>>;
55

6-
export interface HandleAuthOptions {
6+
export type HandleAuthOptions = {
77
returnPathname?: string;
88
onSuccess?: (data: AuthLoaderSuccessData) => void | Promise<void>;
9-
}
9+
} & (
10+
| {
11+
storage?: never;
12+
cookie?: SessionIdStorageStrategy['cookie'];
13+
}
14+
| {
15+
storage: SessionStorage;
16+
cookie: SessionIdStorageStrategy['cookie'];
17+
}
18+
);
1019

1120
export interface AuthLoaderSuccessData {
1221
accessToken: string;

0 commit comments

Comments
 (0)