-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
59 lines (49 loc) · 1.55 KB
/
main.js
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
const express = require("express");
const axios = require("axios");
const dotenv = require("dotenv");
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
const CLIENT_ID = process.env.GITHUB_CLIENT_ID;
const CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET;
const REDIRECT_URI = process.env.GITHUB_REDIRECT_URI;
// Step 1: Redirect user to GitHub for authentication
app.get("/auth/github", (req, res) => {
const githubAuthUrl = `https://github.com/login/oauth/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}`;
res.redirect(githubAuthUrl);
});
try {
// Step 3: Exchange the authorization code for an access token
const response = await axios.post(
"https://github.com/login/oauth/access_token",
{
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
code,
redirect_uri: REDIRECT_URI,
},
{
headers: {
Accept: "application/json",
},
}
);
const accessToken = response.data.access_token;
// Step 4: Use the access token to fetch user data
const userResponse = await axios.get("https://api.github.com/user", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
res.json({
user: userResponse.data,
access_token: accessToken,
});
} catch (error) {
console.error("Error exchanging code for token:", error);
res.status(500).send("Error authenticating with GitHub");
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});