Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Sloppy but it's a working POC. Clerk key would be shared by all vals though, probably annoying since you'd have to configure each URL in clerk. Could be fine though.

The CLERK_JWT_KEY is a hack where I took the pem and replaced all the newlines with '|' so I could set it as an env var and then turn them back to newlines.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* @jsxImportSource https://esm.sh/hono/jsx */
import { Hono } from "npm:hono";
import { html } from "npm:hono/html";
import { Child, type FC } from "npm:hono/jsx";
import { getCookie } from "npm:hono/cookie";
import { jsxRenderer, useRequestContext } from "npm:hono/jsx-renderer";
import { importSPKI, jwtVerify } from "npm:jose";
const clerk_pub_key = Deno.env.get("CLERK_PUB_KEY");
const clerk_jwt_key = Deno.env.get("CLERK_JWT_KEY").replaceAll("|", "\n");
const jwt_key = await importSPKI(clerk_jwt_key, "RS256");
const app = new Hono();
function main(children: Child): JSX.Element {
return (
<main id="main">
<div class="container">{children}</div>
</main>
);
}
function page(children: Child): JSX.Element {
return (
<html>
<head>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
></meta>
<link rel="stylesheet" href="https://unpkg.com/chota@latest"></link>
<script src="https://unpkg.com/htmx.org@1.9.9"></script>
{html`
<script>
async function loadClerk() {
await window.Clerk.load()
if (window.Clerk.user) {
const user_div = document.getElementById('user-button')
user_div.innerHTML = '';
window.Clerk.mountUserButton(user_div);
}
}
</script>
`}
<script async crossorigin="anonymous" data-clerk-publishable-key={clerk_pub_key} onload="loadClerk()"
src="https://guided-buffalo-60.clerk.accounts.dev/npm/@clerk/clerk-js@4/dist/clerk.browser.js"
type="text/javascript">
</script>
</head>
<body>
<nav class="nav" hx-boost="true" hx-target="#main">
<div class="nav-left">
<a class="brand" href="/">
Clerk Example
</a>
</div>
<div class="nav-right">
<div class="tabs">
<div id="user-button"></div>
<a class="button" href="#" onclick="window.Clerk.openSignUp({redirectUrl: '/'})">Sign Up</a>
<a class="button" href="#" onclick="window.Clerk.openSignIn({redirectUrl: '/'})">Sign In</a>
<div id="user-button"></div>
</div>
</div>
</nav>
{main(children)}
</body>
</html>
);
}
const Layout: FC = ({ children }) => {
if (children === undefined) {
return <></>;
}
const { req } = useRequestContext();
const hx_target = req.header("Hx-Target");
if (hx_target === undefined) {
return page(children);
} else if (hx_target === "main") {
return main(children);
} else {
return <>{children}</>;
}
};
const renderer = jsxRenderer(({ children, ...props }) => {
console.log(children);
return <Layout {...props}>{children}</Layout>;
});
app.use("*", renderer);
app.use('*', async (c, next) => {
let user = null;
const session_cookie = getCookie(c, '__session');
if (session_cookie) {
const decoded = await jwtVerify(session_cookie, jwt_key);
user = decoded.payload;
}
saolsen-clerk_hono_poc.web.val.run
December 19, 2023