stevekrouse-lastlogin.web.val.run
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
/** @jsxImportSource npm:hono/jsx */
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { Hono } from "npm:hono";
const { httpEndpoint } = extractValInfo(import.meta.url);
const app = new Hono();
app.get("/", (c) => {
return c.html(<a href="/auth/login">Login</a>);
});
app.get("/auth/login", (c) => {
const url = new URL("https://lastlogin.io/auth");
url.searchParams.set("client_id", httpEndpoint);
url.searchParams.set("redirect_uri", `${httpEndpoint}/auth/callback`);
url.searchParams.set("scope", "email");
url.searchParams.set("response_type", "code");
return c.redirect(url.toString());
});
app.get("/auth/callback", async (c) => {
const code = c.req.query("code");
const url = new URL("https://lastlogin.io/token");
url.searchParams.set("client_id", httpEndpoint);
url.searchParams.set("code", code);
url.searchParams.set("redirect_uri", `${httpEndpoint}/auth/callback`);
const tokenResp = await fetch(url.toString());
const { access_token } = await tokenResp.json() as { access_token: string };
const resp = await fetch("https://lastlogin.io/userinfo", {
headers: {
Authorization: `Bearer ${access_token}`,
},
});
const { email } = await resp.json() as { email: string };
return c.text("Your email is " + email);
});
export default app.fetch;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v0
May 30, 2024