-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (103 loc) 路 3.12 KB
/
Copy pathserver.js
File metadata and controls
122 lines (103 loc) 路 3.12 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const dotenv = require("dotenv");
dotenv.config();
const express = require("express");
const axios = require("axios");
const cors = require("cors");
const app = express();
const port = process.env.PORT || 3000;
// Middleware
const allowedOrigins = [
"https://recodehive.github.io/awesome-github-profiles",
"http://127.0.0.1:5502",
"http://127.0.0.1:5501",
"http://localhost:3000",
"https://example.com"
];
const path = require('path');
app.use(express.static(path.join(__dirname)));
// CORS configuration
app.use(cors({
origin: function (origin, callback) {
// Allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
const msg = 'The CORS policy for this site does not allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
},
credentials: true,
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
allowedHeaders: ['Content-Type', 'Authorization']
}));
app.use(express.json());
// Test route
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// GitHub OAuth route
app.get("/api/auth/github", async (req, res) => {
try {
const { code } = req.query;
if (!code) {
return res.status(400).json({ message: "Authorization code is required" });
}
const response = await axios.post(
'https://github.com/login/oauth/access_token',
{
client_id: process.env.GITHUB_CLIENT_ID,
client_secret: process.env.GITHUB_CLIENT_SECRET,
code: code
},
{
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
}
);
if (response.data.error) {
throw new Error(response.data.error_description || response.data.error);
}
res.json({ data: response.data });
} catch (error) {
console.error('GitHub OAuth error:', error.response?.data || error);
res.status(error.response?.status || 500).json({
message: "Failed to authenticate with GitHub",
error: error.message
});
}
});
// Get GitHub user data route
app.get("/api/auth/github/getUser", async (req, res) => {
try {
const authHeader = req.get("Authorization");
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ message: "Invalid authorization header" });
}
const token = authHeader.split(' ')[1];
const response = await axios.get("https://api.github.com/user", {
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/json'
}
});
res.status(200).json({
message: "success",
user: {
name: response.data.name,
email: response.data.email
}
});
} catch (error) {
console.error('GitHub user data error:', error.response?.data || error);
res.status(error.response?.status || 500).json({
message: "Failed to fetch user data",
error: error.message
});
}
});
// Start server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});