Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
/** @jsxImportSource npm:hono/jsx */
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { Hono } from "npm:hono";
import { deleteCookie, getCookie, getSignedCookie, setCookie, setSignedCookie } from "npm:hono/cookie";
const app = new Hono();
const val = extractValInfo(import.meta.url);
const clientID = new URL("/", val.httpEndpoint).toString();
const redirectURI = new URL("/redirect", val.httpEndpoint).toString();
app.get("/", (c) => {
return c.json("OK");
});
app.get("/login", (c) => {
const state = crypto.randomUUID();
setCookie(c, "state", state, {
httpOnly: true,
});
const url = new URL("https://pomdtr-github_oauth.web.val.run");
// TODO: add state to cookies
return c.html(
<form action="https://pomdtr-indielogin.web.val.run" method="get">
<label for="url">Web Address:</label>
<input id="url" type="text" name="me" placeholder="yourdomain.com" />
<p>
<button type="submit">Sign In</button>
</p>
<input type="hidden" name="client_id" value={clientID} />
<input type="hidden" name="redirect_uri" value={redirectURI} />
<input type="hidden" name="state" value={redirectURI} />
</form>,
);
});
app.get("/redirect", (c) => {
const query = c.req.query();
const cookie = getCookie(c);
if (cookie.state != query.state) {
return new Response(null, {
status: 500,
});
}
const user = fetch("https://pomdtr-indielogin.web.val.run", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"Accept": "application/json",
},
body: new URLSearchParams({
"code": query.code,
"client_id": clientID,
"redirect_uri": redirectURI,
}),
});
return c.json("OK");
});
export default app.fetch;
pomdtr-indielogin_example.web.val.run
April 14, 2024