-
Notifications
You must be signed in to change notification settings - Fork 210
/
initMiro.ts
44 lines (41 loc) · 1.16 KB
/
initMiro.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
39
40
41
42
43
44
import { Miro } from "@mirohq/miro-api";
import { serialize } from "cookie";
function getSerializedCookie(name: string, value: string) {
return serialize(name, value, {
path: "/",
httpOnly: true,
sameSite: "none",
secure: true,
});
}
export default function initMiro(
request: { cookies: Record<string, undefined | string> },
response?: { setHeader(name: string, value: string[]): void },
) {
const tokensCookie = "miro_tokens";
// setup a Miro instance that loads tokens from cookies
return {
miro: new Miro({
storage: {
get: () => {
// Load state (tokens) from a cookie if it's set
try {
return JSON.parse(request.cookies[tokensCookie] || "null");
} catch (err) {
return null;
}
},
set: (_, state) => {
if (!response)
throw new Error(
"initMiro should be invoked with a response object",
);
// store state (tokens) in the cookie
response.setHeader("Set-Cookie", [
getSerializedCookie(tokensCookie, JSON.stringify(state)),
]);
},
},
}),
};
}