-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
60 lines (49 loc) · 1.4 KB
/
server.ts
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
// server.ts
import express from "express";
import cors from "cors";
import axios from "axios";
import * as dotenv from "dotenv";
import { fileURLToPath } from "url";
import { dirname } from "path";
import path from "path";
// ES modules fix for __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Load environment variables
dotenv.config();
const app = express();
app.use(cors());
app.use(express.json());
const clientId = process.env.GITHUB_CLIENT_ID;
const clientSecret = process.env.GITHUB_CLIENT_SECRET;
if (!clientId || !clientSecret) {
console.error("Missing required environment variables");
process.exit(1);
}
app.post("/api/github/token", async (req, res) => {
const { code } = req.body;
if (!code) {
return res.status(400).json({ error: "Code is required" });
}
try {
const response = await axios.post(
"https://github.com/login/oauth/access_token",
{
client_id: clientId,
client_secret: clientSecret,
code,
},
{
headers: { Accept: "application/json" },
},
);
res.json(response.data);
} catch (error) {
console.error("Token exchange error:", error);
res.status(500).json({ error: "Failed to exchange code for token" });
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});