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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export async function handleSanguineGithubWebhook(
req: express.Request,
res: express.Response,
) {
// check custom auth secret sent from clerk
console.log("New gh POST req");
console.email(
`you just got a Github Webhook event dog. \n ${JSON.stringify(req)}`,
"Github Webhook on Sanguine extension",
);
const code = req?.query?.code;
if (!code) {
res.status(500).send("No code arrived!");
}
console.log("code: ", code);
try {
const gh_res = await fetch(
`https://github.com/login/oauth/access_token?code=${code}&client_id=${process.env.sanguineGithubClientId}&client_secret=${process.env.sanguineGithubClientSecret}`,
{
method: "POST",
headers: {
"Accept": "*/*",
"Host": "github.com",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Content-Length": "0",
},
},
);
console.log("made it past fetch");
console.log("gh_res: ", gh_res);
if (!gh_res.ok) {
throw new Error(`Failed to fetch`);
}
const gh_res_json = await gh_res.json();
console.log("gs_res_json: ", gh_res_json);
const data = gh_res_json?.data;
console.log("data: ", data);
const accessToken = data?.access_token;
console.log("access_token: ", data);
// // Fetch user information using the access token
const userResponse: any = await fetch("https://api.github.com/user", {
headers: {
"Authorization": `Bearer ${accessToken}`,
"User-Agent": "sanguine-extension",
},
});
console.log("userResponse: ", userResponse);
// // The user's GitHub data is now available in userResponse.data
// // You can now use this data to sign the user in, create a user in your own system, etc.
res.json(userResponse?.data);
}
catch (err) {
console.error("Error fetching access token or user data:", err);
res.status(500).send("Authentication failed");
}
}
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!
v115
October 23, 2023