-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathinitMiro.ts
49 lines (45 loc) · 1.31 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
45
46
47
48
49
import { Miro } from "@mirohq/miro-api";
import { serialize } from "cookie";
function getSerializedCookie(name: string, value: string) {
const expires = new Date();
expires.setFullYear(expires.getFullYear() + 2);
return serialize(name, value, {
path: "/",
httpOnly: true,
sameSite: "none",
secure: true,
expires,
});
}
export default function initMiro(
request: { cookies: Record<string, undefined | string> },
response?: { setHeader(name: string, value: string[]): void },
) {
const tokensCookie = "miro_tokens";
// Set up a Miro instance that loads tokens from cookies
return {
miro: new Miro({
storage: {
get: () => {
// If the state (tokens) is set, load it from a cookie
try {
return JSON.parse(request.cookies[tokensCookie] || "null");
} catch (err) {
return null;
}
},
set: (_userId, state) => {
if (!response)
throw new Error(
"initMiro should be invoked with a response object",
);
// Store the state (tokens) in the cookie
console.log("Setting cookies");
response.setHeader("Set-Cookie", [
getSerializedCookie(tokensCookie, JSON.stringify(state)),
]);
},
},
}),
};
}