-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (38 loc) · 1.41 KB
/
server.js
File metadata and controls
46 lines (38 loc) · 1.41 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
require('dotenv').config();
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
const redirectUri = process.env.REDIRECT_URI;
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3000; // Choose a port for your server
// Redirect to GitHub for authentication
app.get('/auth', (req, res) => {
res.redirect(`https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}`);
});
// GitHub callback to exchange code for access token
app.get('/auth/callback', async (req, res) => {
const code = req.query.code;
try {
const { data } = await axios.post('https://github.com/login/oauth/access_token', {
client_id: clientId,
client_secret: clientSecret,
code: code,
redirect_uri: redirectUri,
}, {
headers: {
'Accept': 'application/json',
},
});
// Handle the access token (e.g., save it to session or database)
const accessToken = data.access_token;
// For demonstration purposes, you may want to redirect to a success page
res.redirect('https://cotharticren.wixsite.com/dartmeadow/autumn');
} catch (error) {
console.error('Error exchanging code for access token:', error);
res.status(500).send('Error exchanging code for access token');
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});