-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (108 loc) · 4.02 KB
/
index.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
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
const express = require("express");
const bodyParser = require("body-parser");
const FormData = require("form-data");
const fetch = require("node-fetch");
const axios = require("axios");
const app = express();
const cors = require("cors");
app.use(bodyParser.json());
app.use(bodyParser.json({ type: "text/*" }));
app.use(bodyParser.urlencoded({ extended: false }));
// app.use(cors());
// Enabled Access-Control-Allow-Origin", "*" in the header so as to by-pass the CORS error.
// app.use((req, res, next) => {
// res.header("Access-Control-Allow-Origin", "*");
// next();
// });
const doAsync = (fn) => async (req, res, next) => await fn(req, res, next).catch(next);
async function err() {
throw new Error("에러 발생");
}
//라우팅 핸들러
app.post(
"/endpoint/authenticate",
doAsync(async (req, res) => {
console.log("Request on");
const { code } = req.body;
console.log(code);
const response = await axios
.post("https://github.com/login/oauth/access_token", {
code: code,
client_id: "aac6cc06d7890685988f",
client_secret: "0c85c443181245cc6fba6e3d41e184e678c40d0c",
})
.then((response) => {
result = response.data;
accessTokenRaw = response.data.split("&")[0];
accessToken = accessTokenRaw.split("=")[1];
console.log(result);
const responseJson = {
accessToken: accessToken,
};
return responseJson;
})
.catch((error) => {
console.log("error");
return "error";
});
if (response) {
res.json(response);
}
})
);
// app.post("/endpoint/authenticate", (req, res) => {
// console.log("Request on");
// const { code } = req.body;
// console.log(code);
// const client_secret = "0c85c443181245cc6fba6e3d41e184e678c40d0c";
// const client_id = "aac6cc06d7890685988f";
// const data = new FormData();
// data.append("client_id", client_id);
// data.append("client_secret", client_secret);
// data.append("code", code);
// var result = null;
// axios
// .post("https://github.com/login/oauth/access_token", {
// code: code,
// client_id: "aac6cc06d7890685988f",
// client_secret: "0c85c443181245cc6fba6e3d41e184e678c40d0c",
// })
// .then((response) => {
// console.log(response);
// res.send(json(response.data));
// result = response.data;
// return res.status(200).json(response.data);
// })
// .catch((error) => {
// console.log("error");
// return res.status(401);
// });
// // Request to exchange code for an access token
// // fetch(`https://github.com/login/oauth/access_token`, {
// // method: "POST",
// // body: data,
// // })
// // .then((response) => {
// // console.log(response);
// // response.text();
// // })
// // .then((paramsString) => {
// // let params = new URLSearchParams(paramsString);
// // const access_token = params.get("access_token");
// // const scope = params.get("scope");
// // const token_type = params.get("token_type");
// // // Request to return data of a user that has been authenticated
// // return fetch(
// // `https://api.github.com/user?access_token=${access_token}&scope=${scope}&token_type=${token_type}`
// // );
// // })
// // .then((response) => response.json())
// // .then((response) => {
// // return res.status(200).json(response);
// // })
// // .catch((error) => {
// // return res.status(400).json(error);
// // });
// });
const PORT = process.env.SERVER_PORT || 5000;
app.listen(PORT, () => console.log(`Listening on ${PORT}`));