-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (47 loc) · 1.49 KB
/
Copy pathserver.js
File metadata and controls
52 lines (47 loc) · 1.49 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
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID;
const GITHUB_SCOPE = process.env.GITHUB_SCOPE || 'repo,user';
// Proxy for requesting device code
app.post('/api/github/device/code', async (req, res) => {
try {
const response = await axios.post(
'https://github.com/login/device/code',
new URLSearchParams({
client_id: GITHUB_CLIENT_ID,
scope: GITHUB_SCOPE,
}),
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
);
res.json(response.data);
} catch (err) {
res.status(500).json({ error: err.toString() });
}
});
// Proxy for polling access token
app.post('/api/github/device/token', async (req, res) => {
const { device_code } = req.body;
try {
const response = await axios.post(
'https://github.com/login/oauth/access_token',
new URLSearchParams({
client_id: GITHUB_CLIENT_ID,
device_code,
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
}),
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json' } }
);
res.json(response.data);
} catch (err) {
res.status(500).json({ error: err.toString() });
}
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Proxy server running on port ${PORT}`);
});