forked from vendure-ecommerce/storefront-remix-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.ts
38 lines (36 loc) · 1.1 KB
/
sessions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {
IS_CF_PAGES,
safeRequireNodeDependency,
} from '~/utils/platform-adapter';
import { SessionStorage } from '@remix-run/server-runtime/dist/sessions';
import { ErrorResult } from '~/generated/graphql';
import { createCookieSessionStorage } from '@remix-run/cloudflare';
import { CreateCookieSessionStorageFunction } from '@remix-run/server-runtime';
async function getCookieSessionStorageFactory(): Promise<CreateCookieSessionStorageFunction> {
if (IS_CF_PAGES) {
return createCookieSessionStorage;
} else {
return safeRequireNodeDependency('@remix-run/node').then(
(module) => module.createCookieSessionStorage,
);
}
}
let sessionStorage: SessionStorage<
{ activeOrderError: ErrorResult } & Record<string, any>
>;
export async function getSessionStorage() {
if (sessionStorage) {
return sessionStorage;
}
const factory = await getCookieSessionStorageFactory();
sessionStorage = factory({
cookie: {
name: 'vendure_remix_session',
httpOnly: true,
path: '/',
sameSite: 'lax',
secrets: ['awdbhbjahdbaw'],
},
});
return sessionStorage;
}