-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.tsx
52 lines (45 loc) · 1.4 KB
/
server.tsx
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { cookies } from 'next/headers';
import { CookieAttributes, Cookies } from './types';
import type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies';
import React, { FC, ReactNode } from 'react';
import { SecureCookiesProvider } from './provider';
import { storeSecureCookies } from './secure';
export const CookiesProvider: FC<{ children: ReactNode }> = ({ children }) => {
const secretValue = cookies().getAll();
return (
<SecureCookiesProvider value={storeSecureCookies(secretValue)}>
{children}
</SecureCookiesProvider>
);
};
export const getCookies = (): Cookies => {
const org = cookies();
return {
get: (name?: string) =>
(name == null
? Object.fromEntries(org.getAll().map((c) => [c.name, c.value]))
: org.get(name)?.value) as never,
set: (name, value, options) => {
const pre = org.get(name)?.value;
org.set(name, value, options && convertCookieAttributes(options));
return pre;
},
remove: (name) => org.delete(name),
};
};
const convertCookieAttributes = (
options: CookieAttributes,
): Partial<ResponseCookie> => ({
expires:
typeof options.expires === 'number'
? options.expires * 864e5
: options.expires,
path: options.path,
domain: options.domain,
secure: options.secure,
sameSite: options.sameSite?.toLowerCase() as
| 'strict'
| 'lax'
| 'none'
| undefined,
});