-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathinitMiro.ts
47 lines (44 loc) · 1.28 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
import { Miro } from "@mirohq/miro-api";
import { serialize } from "cookie";
function getSerializedCookie(name: string, value: string | number) {
return serialize(name, String(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";
const userIdCookie = "miro_user_id";
// 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: (userId, state) => {
// store state (tokens) in the cookie
response &&
"setHeader" in response &&
response.setHeader("Set-Cookie", [
getSerializedCookie(tokensCookie, JSON.stringify(state)),
getSerializedCookie(userIdCookie, userId),
]);
},
},
}),
userId: JSON.parse(request.cookies[tokensCookie] || "null")?.userId,
};
}