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
// This approach adds Lucia authentication with signup to the existing Excalidraw implementation.
// We'll use the luciaMiddleware to handle authentication and redirect unauthenticated users.
// The Excalidraw functionality will only be accessible to authenticated users.
import { html } from "https://esm.sh/common-tags@1.8.2";
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=29";
import { blob } from "https://esm.town/v/std/blob?v=12";
import { luciaMiddleware } from "https://esm.town/v/stevekrouse/lucia_middleware_safe";
import { createExcalidraw } from "https://jsr.io/@smallweb/excalidraw/0.3.0/mod.ts";
import { join } from "jsr:@std/path@0.225.2";
const { name } = extractValInfo(import.meta.url);
const excalidrawHandler = createExcalidraw({
store: {
get: async (key) => {
try {
const res = await blob.get(join(name, key));
return new Uint8Array(await res.arrayBuffer());
} catch (e) {
return null;
}
},
set: (key, value) => {
return blob.set(join(name, key), value);
},
},
});
export default luciaMiddleware(async function server(request: Request): Promise<Response> {
const username = request.headers.get("X-Lucia-Username");
const url = new URL(request.url);
if (!username) {
return new Response(null, {
status: 302,
headers: { Location: "/auth/signup" },
});
}
// User is authenticated, proceed with Excalidraw
return excalidrawHandler(request);
});