-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
77 lines (66 loc) · 1.96 KB
/
main.mjs
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
import express from "express"
import FormData from "form-data";
import axios from "axios";
import request from "request";
const app = express()
const CLIENT_SECRET = process.env.CLIENT_SECRET
const CLIENT_ID = process.env.CLIENT_ID
const REDIRECT_URI = process.env.REDIRECT_URI
if (!CLIENT_SECRET) {
throw new Error("SECRET NOT PROVIDED!")
}
app.get('/v1/after-login', async (req, res) => {
const code = req.query.code
if (!code) {
res.status(400).send("code query is not contain in your URL.")
return
}
const options = {
'method': 'POST',
'url': 'https://discordapp.com/api/oauth2/token',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded',
},
form: {
'code': code,
'redirect_uri': REDIRECT_URI,
'client_secret': CLIENT_SECRET,
'client_id': CLIENT_ID,
'grant_type': 'authorization_code',
'scope': 'identify'
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
res.redirect(`http://localhost:58694/v1/auth-redirect?json=${response.body}`)
// res.send(response.body)
return
});
})
app.post("/v1/refresh", async (req, res) => {
const refresh_token = req.query.refresh
if (!refresh_token) {
res.status(400).send("refresh token is not include your url.")
return
}
const options = {
'method': 'POST',
'url': 'https://discordapp.com/api/oauth2/token',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded',
},
form: {
'refresh_token': refresh_token,
'redirect_uri': REDIRECT_URI,
'client_secret': CLIENT_SECRET,
'client_id': CLIENT_ID,
'grant_type': 'refresh_token',
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
res.redirect(`http://localhost:58694/v1/auth-redirect?json=${response.body}`)
return
});
})
app.listen(3000)