-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (42 loc) · 1.28 KB
/
Copy pathindex.js
File metadata and controls
56 lines (42 loc) · 1.28 KB
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
require("dotenv").config();
const express = require("express");
const app = express();
const path = require("path");
const axios = require("axios");
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "static", "index.html"));
});
app.get("/auth", (req, res) => {
res.redirect(
`https://github.com/login/oauth/authorize?client_id=${process.env.CLIENT_ID}`
);
});
app.get("/oauth/callback", (req, res) => {
try {
const body = {
client_id:process.env.CLIENT_ID,
client_secret:process.env.CLIENT_SECRET,
code: req.query.code,
};
const opts = { headers: { accept: "application/json" } };
axios
.post("https://github.com/login/access_token", body, opts)
.then((_res) => {
console.log("Response from GitHub:", _res.data);
return _res.data.access_token;
})
.then((token) => {
if (!token) throw new Error("Token not received");
res.redirect(`/?token=${token}`);
})
.catch((err) => {
console.error(`Error fetching access token: ${err.response?.data || err.message}`);
res.status(500).send("Authentication failed");
});
} catch (error) {
console.log(`There as an error ${error}`);
}
});
app.listen(3000, () => {
console.log(`The application is running on PORT ${3000}`);
});