-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.tsx
55 lines (47 loc) · 1.35 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
53
54
55
import { createServer } from "ultra/server.ts";
import { loadSync } from "std/dotenv/mod.ts";
import {
createGitHubOAuth2Client,
getSessionAccessToken,
getSessionId,
handleCallback,
signIn,
signOut,
} from "kv_oauth";
import App from "./src/app.tsx";
loadSync({ export: true });
const oauth2Client = createGitHubOAuth2Client();
const server = await createServer({
importMapPath: import.meta.resolve("./importMap.json"),
});
function ServerApp({ accessToken, isSignedIn }: {
accessToken: string | null;
isSignedIn: boolean;
}) {
return <App {...{ accessToken, isSignedIn }} />;
}
server
.get("/", async (ctx) => {
const sessionId = await getSessionId(ctx.req.raw);
const isSignedIn = sessionId != null;
const accessToken = isSignedIn
? await getSessionAccessToken(oauth2Client, sessionId)
: null;
const result = await server.render(
<ServerApp {...{ accessToken, isSignedIn }} />,
);
return ctx.body(result, 200, {
"content-type": "text/html; charset=utf-8",
});
})
.get("/signin", async (ctx) => {
return await signIn(ctx.req.raw, oauth2Client);
})
.get("/callback", async (ctx) => {
const { response } = await handleCallback(ctx.req.raw, oauth2Client);
return response;
})
.get("/signout", async (ctx) => {
return await signOut(ctx.req.raw);
});
Deno.serve(server.fetch);