-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (58 loc) · 1.8 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
const express = require('express')
const app = express()
const axios = require('axios')
const port = 3030
const {
clientId,
secret,
}= require('./keys')
const REDIRECT_URI = 'http://localhost:3000'
//TODO: move this to the server
const userName = clientId
const password = secret
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "http://localhost:3000"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
const postData = async (baseURL, path, data = {}) => {
const options = {
baseURL,
url: path,
method: 'post',
headers: {
// 'Content-Type': 'application/json',
'Authorization': 'Basic ' + Buffer.from(userName + ":" + password).toString('base64'),
'Content-Type': 'application/x-www-form-urlencoded',
},
data
}
return axios(options).then(res => res).catch(e => e)
}
app.get('/login', (req, res) => {
const {
query: {
code = ''
},
} = req;
(async () => {
const body = `grant_type=authorization_code&code=${code}&redirect_uri=${REDIRECT_URI}`
const token = await postData('https://www.reddit.com', '/api/v1/access_token', body);
const {
status,
data,
} = token;
const {
error,
} = data;
if (status === 200) {
if (error === 'invalid_grant') return res.status(401).json(data)
res.status(200).json(data)
} else {
res.status(500)
}
})();
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})